How can I manually add a legend to my line plot? (error message "scale for 'y' is already present") - plot

I have produced a line plot to compare results under three conditions. These were all under separate dataframes. Here is the code for the plot:
ggplot()+
geom_line(data = linedatac1data, aes(x=time, y=c1y), size=1.2, colour=rgb(0.6,0.9,0.9))+
geom_line(data = linedatac2data, aes(x=time, y=c2y), size=1.2, colour=rgb(0.9,0,0.9))+
geom_line(data = linedataidata, aes(x=time, y=Iy), size=1.2, colour=rgb(0,0.5,0.5))+
xlim(-350,1500)+
ylim(-2.6e-23,3.1e-23)+
theme_light()+
scale_y_continuous(breaks = c(-2e-23, -1e-23, 0, 1e-23, 2e-23, 3e-23))+
ggtitle("Changes in Mean Theta Power Across Conditions")+
xlab("Time (ms)")+
ylab("Mean theta power ( mV^2)")
I would like to manually produce a key matching the names of each condition (C1, C2, and I) are matched to their corresponding RGBs.
I attempted to do this by adding the following code:
scale_colour_manual("",
breaks = c("Single control", "Double control", "Inhibition"),
values = c("Single control"=rgb(0.6,0.9,0.9), "Double control"=rgb(0.9,0,0.9), "Inhibition"=rgb(0,0.5,0.5)))
However, I am met with an error message which reads:
"Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale."
Any help would be much appreciated. Thanks!

Related

How to combine colour and linetype into a single legend?

I am trying to combine two legends of unequal length into a single length and am having some problems. I am using colour to represent 3 different populations, whereas I'm using linetype to show whether the population is infected or uninfected with particular bacteria.
This is what my code looked like to begin with:
ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Population)) +
geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
geom_line(aes(linetype=Infection_Status))+
geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") +
scale_x_continuous(n.breaks=6)
and the resulting graph:
Following some advice from some other questions on here, I made several attempts to fix the issue, but was unsuccessful. First I tried manually labelling the legend:
ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Line)) +
geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
geom_line()+
geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") +
scale_x_continuous(n.breaks=6) +
scale_colour_manual(name ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values = c("blue", "blue", "green", "green", "red", "red")) +
scale_linetype_manual(name ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values=c("solid", "dotted", "solid", "dotted", "solid", "dotted"))
while this had the desired effect for colour, for some reason I no longer had dotted lines for my uninfected lines. In attempting to reinclude them, I ended up with this:
ggplot(sds, aes(x= Temp, y=Fertile, group=Line, color=Line)) +
geom_errorbar(aes(ymin=Fertile-se, ymax=Fertile+se), width=.1) +
geom_line(aes(linetype=Infection_Status))+
geom_point()+labs(title="Fertility Thermal Limits", x= "Temperature", y= "Proportion of viable mating pairs") +
scale_x_continuous(n.breaks=6) +
scale_colour_manual(name ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values = c("blue", "blue", "green", "green", "red", "red")) +
scale_linetype_manual(name ="Line",labels=c("Nowra +", "Nowra -", "Brisbane +", "Brisbane -", "Cairns +", "Cairns -"), values=c("solid", "dotted", "solid", "dotted", "solid", "dotted"))
The graph is back to normal, but the legend is still wrong. Does anyone have any advice?
Without your data or a representative example, I cannot give you a solution for your particular question; however, one approach you could take is exemplified here using the CO2 built-in dataset. In this example, you have a similar situation:
library(ggplot2)
ggplot(CO2, aes(x=conc, y=uptake, color=Type, linetype=Treatment)) +
stat_summary(geom="line") +
stat_summary(geom="point")
Aesthetics in ggplot2 are mapped to columns in your dataset. The example above and in your own dataset, you have two aesthetics (color and linetype) mapped to two separate columns in your data. Instead of trying to kind of "recombine" those columns into one to make one legend within the plot code, I would recommend setting up a new column in the dataset itself, then map to that new column and manually assign values.
In the CO2 dataset example, this might look something like this:
library(dplyr)
df <- CO2 %>% mutate(newcol = paste(Type, "-", Treatment))
ggplot(df, aes(x=conc, y=uptake, color=newcol, linetype=newcol)) +
stat_summary(geom="line") +
stat_summary(geom="point") +
scale_color_manual(values=c("red", "red", "blue", "blue")) +
scale_linetype_manual(values=c(1,2,1,2))
The idea is to create a new column (newcol) from concatenating the values of the other two columns (Type and Treatment). The plot code is then modified to map color and linetyp to the newly-created column in the dataset. This by itself puts everything into one legend, and you can modify the scale of the two aesthetics via scale_*_manual(). In this example, I am just supplying a standard vector to the values= argument, but if you want to explicitly map a particular color or linetype to a particular value of the newly-created column, you can use a named vector.

How to create a legend title in a ggplot2 line graph

I am creating a line graph using the ggplot2 package in R.
I cannot upload the data as it is for a study I am conducting for my final year project. So, I can only share the code with you.
This is the code for the APA formatted graph.
ggplot(accuracy_data,
aes(x = eccentricity, y = accuracy, group= speech_task)) +
geom_line(aes(linetype=speech_task)) +
scale_linetype_manual(values=c("twodash", "dotted", "solid")) +
geom_point(aes(shape = speech_task)) +
facet_grid(. ~ duration, labeller=labeller(duration = labels)) +
labs(x='Eccentricity (degrees of visual angle)', y='Accuracy of responses') +
theme_apa() +
theme(text=element_text(family='Times')) +
scale_x_continuous(breaks =c(5, 10, 15)) +
geom_errorbar(aes(ymin = accuracy - acc_sum$se , ymax = accuracy + acc_sum$se ), width=.1)
This produces a graph with a legend without a title, hence I am asking for help in creating a title for the legend.
I have tried a lot of different options however none work. I don't even get an error message.
These are the codes I have tried so far:
legend_title <- "Speech Task"
scale_fill_manual(legend_title,values=c("Conversation", "N-Back", "Silence"))
guides(fill=guide_legend(title="Speech Task"))
scale_fill_discrete(name = "Speech Task",
labels = c("Conversation", "N-Back", "Silence"))
labs(fill="Speech Task")
The following and final code I tried was the only one to produce a change in the graph. However because I have manually changed the point shape as well as line type it caused two legends to be made and only titled the line type legend.
labs(linetype= "Speech Task")
Please can I have some help :)
Seeing no data or final results I'm going on a hunch here.
I suspect you need to name shape and fill legends the same. So something along the lines of
scale_linetype_manual(name = legend_title, values = c("twodash", "dotted", "solid")) +
scale_fill_manual(name = legend_title, values = c("Conversation", "N-Back", "Silence")) +

ggplot error: Making line graphs of three variables, then making a facet plot of those

I'm trying to make a facet plot of three line plots, and in each line plot there are three lines. I'd like to make the line corresponding to "oirf" to be a solid line, and both lines corresponding to "upperCI" and "lowerCI" to be dashed, either the same kind of dashed line or something very similar looking, like so:
But I'm getting an error I can't resolve.
Here's my data:
"countrySpellId","iso","country","step","indicator","vals"
38,"BLR","Belarus",0,"oirf",-0.19979745478058
38,"BLR","Belarus",1,"oirf",-0.182586795026907
38,"BLR","Belarus",2,"oirf",-0.242312010909111
106,"GEO","Georgia",0,"oirf",-0.154580915298088
106,"GEO","Georgia",1,"oirf",-0.0572862343086547
106,"GEO","Georgia",2,"oirf",-0.345457860190889
167,"KGZ","Kyrgyzstan",0,"oirf",0.960777168532119
167,"KGZ","Kyrgyzstan",1,"oirf",0.458003383067036
167,"KGZ","Kyrgyzstan",2,"oirf",0.190725669245905
38,"BLR","Belarus",0,"lowerCI",-0.357909781851253
38,"BLR","Belarus",1,"lowerCI",-0.411483619567094
38,"BLR","Belarus",2,"lowerCI",-0.514508124910321
106,"GEO","Georgia",0,"lowerCI",-0.323219475085121
106,"GEO","Georgia",1,"lowerCI",-0.236286319570866
106,"GEO","Georgia",2,"lowerCI",-0.540228716700013
167,"KGZ","Kyrgyzstan",0,"lowerCI",0.448913075973564
167,"KGZ","Kyrgyzstan",1,"lowerCI",0.0581860926615476
167,"KGZ","Kyrgyzstan",2,"lowerCI",-0.235580302805703
38,"BLR","Belarus",0,"upperCI",-0.0416851277099078
38,"BLR","Belarus",1,"upperCI",0.0463100295132811
38,"BLR","Belarus",2,"upperCI",0.0298841030920997
106,"GEO","Georgia",0,"upperCI",0.0140576444889454
106,"GEO","Georgia",1,"upperCI",0.121713850953557
106,"GEO","Georgia",2,"upperCI",-0.150687003681766
167,"KGZ","Kyrgyzstan",0,"upperCI",1.47264126109067
167,"KGZ","Kyrgyzstan",1,"upperCI",0.857820673472524
167,"KGZ","Kyrgyzstan",2,"upperCI",0.617031641297513
Here's how I'm trying to do the plots:
ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, group = countrySpellId,
stat = "identity")) +
geom_line(aes(linetype = indicator)) +
xlab("Month") + ylab("Percent change") +
theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
scale_linetype_discrete(name ="indicator",
breaks=c("lowerCI", "oirf","upperCI")) +
facet_wrap( ~ country, scales = "free_y", nrow = 3 )
But then I get this error, which is somehow related to the aes(linetype = indicator) I think.
Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line
What am I doing wrong?
As Richard pointed out, deleting the group=countrySpellId will eliminate the error, because you are trying to apply a group twice on two different variables (the linetype argument essentially does the same thing as group, it just means that that the different lines will also have different linetypes). The grouping on country will happen later in the facet_wrap.
Just doing that will get you three different linetypes that ggplot automatically chooses for you, but since you're particular about how those lines should look, you'll want to use scale_linetype_manual, which allows you to specify what linetype ggplot assigns to each factor level. You were on the right tract with scale_linetype_discrete!
Deleting the bad group argument and replacing the scale_lientype_discrete we have:
ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, stat = "identity")) +
geom_line(aes(linetype = indicator)) +
xlab("Month") + ylab("Percent change") +
theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
scale_linetype_manual(name = "indicator", values = c(2,1,2)) +
facet_wrap( ~ country, scales = "free_y", nrow = 3 )

None-missing rows were removed in geom_point in ggplot

Why the rows in this data was claimed to be missing and removed in the plot even though the x-scale isn't out of range? I have tried to include xlim without success. What do I miss here? This is the figure Gp2 (geom_point) isn't included in the plot. The code I used is as follows:
df1 <- data.frame(x=c(2,4:8),
y=c(1.030928,4.123711,3.092784,8.247423,9.278351,4.123711))
df2 <- data.frame(x=3:8,
y=c(1.700680,1.360544,4.081633,3.401361,3.061224,9.183673))
require(ggplot2)
ggplot(NULL, aes(x=x, y=y)) +
geom_bar(data = df1, aes(fill="Gp1", shape="Gp1"),
stat= "identity") +
geom_point(data = df2, stat= "identity", size = 5,
aes(shape="Gp2", fill="Gp2")) +
ylab("%") + xlab("grades") +
ggtitle("Test figure") +
scale_shape_manual(values = c(23, NA)) +
scale_fill_manual(values = c("#6699CC","#000099")) +
guides(fill = guide_legend(reverse = TRUE),
shape = guide_legend(override.aes = list(shape=0), reverse = TRUE))
This gives warning message:
Removed 6 rows containing missing values (geom_point).
Running your code piece-by-piece we can easily find that scale_shape_manual is the culprit here, everything before that works find. (If you had made a minimal example, you would have easily found that..)
You have told ggplot that all the shapes for geom_point should be Gp2, which is the second shape you have mapped. So it will look at the second entry in values and find there is an NA. So you yourself told ggplot that it should give NA shapes to all points.
(Note that you mapped shape Gp1 in geom_bar, but geom_bar doesn't take that aesthetic..)

R - ggplot geom_step error

I am trying to plot a graph of 3 observations (infusion rate, sedation level, tolerance level) as a function of time.
I have data for a hundred patients, which I want to see in separate panels. My data frame has the sedation level and tolerance level data in the same column, "Observation", while the infusion rate is in another column, "Rate". The three observations are grouped by the column "Subtype".
So, far I can plot scatter plot and line with this code:
p <- ggplot(data, aes(group = Subtype, colour = Subtype)) + facet_wrap(~ Pt_ID, ncol = 6) + geom_point(aes(Time, Observation)) + geom_point(aes(Time, Rate), colour = "pink") + geom_line(aes(Time, Rate)) + geom_line(aes(Time, Observation))
However the graph is not what I wanted, I need to get a step plot.
But when I use geom_step function with this code:
p <- ggplot(data, aes(group = Subtype, colour = Subtype)) + facet_wrap(~ Pt_ID, ncol = 6) + geom_step(aes(Time, Observation)) + geom_step(aes(Time, Rate), colour="pink")
I received this error message:
Error in grid.Call.graphics(L_lines, x$x, x$y, index, x$arrow) :
invalid line type
I am rather new with R. If anyone can tell me what is wrong with my code or any suggestions, I'd really really appreciate it.

Resources