Adding a Legend in ggplot2 - r

I have the following code.
Financial_Wealth.lq,Financial_Wealth.uq,Total_Wealth.lq,Total_Wealth.uq,time=seq(0,(sPar.dNN),1))
ggplot(data, aes(x=time)) +
geom_line(aes(y = Human_Capital.mean), color="red", size=1) +
geom_line(aes(y = Financial_Wealth.mean), color="goldenrod3", size=1) +
geom_ribbon(aes(ymin=Financial_Wealth.lq, ymax = Financial_Wealth.uq), alpha=0.4, fill="goldenrod3") +
geom_line(aes(y = Total_Wealth.mean), color="dodgerblue", size=1)+
geom_ribbon(aes(ymin=Total_Wealth.lq, ymax=Total_Wealth.uq), alpha=0.4, fill = "dodgerblue") +
scale_x_continuous(name = 'Age',
breaks=(c(seq(0,(sPar.dNN),4))))+
scale_y_continuous(name = 'Wealth Level',
breaks = seq(0,100,10))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.title = element_text(size=12, face="bold"),
legend.text = element_text(size=12),
axis.title = element_text(size=12),
axis.text = element_text(size=10)) +
coord_cartesian(xlim = c(0,45), ylim = c(0,100), expand = TRUE)+
scale_fill_manual(name="Median",values=c("goldenrod3", "red","dodgerblue"),
labels = c("Financial Wealth", "Human Capital", "Total Wealth"))+
ggtitle('Optimal Wealth Development')
You can interpret each data input as a vector of numbers of equal length. Can someone please tell me why the legend is not appearing? What do I need to do differently! Thanks in advance :) I have attached the image that it is producing so you get an idea of what I am trying to achieve.

In order to add a legend, you need to specify one of the aesthetics within aes(). In this case, take all of your geom_line() calls and place for each one the color= inside of aes(). The value assigned to color= within aes() will be the text of the label in the legend: not the color. To assign color, you need to add scale_color_manual() and set values= a named vector.
See below for the following changes that should solve your problem, although in the absence of your dataset or a reprex, I'm unable to test the function of the new code.
# original code
... +
geom_line(aes(y = Human_Capital.mean), color="red", size=1) +
geom_line(aes(y = Financial_Wealth.mean), color="goldenrod3", size=1) +
geom_line(aes(y = Total_Wealth.mean), color="dodgerblue", size=1)+
# new plot code
... +
geom_line(aes(y = Human_Capital.mean, color="Human Capital Mean"), size=1) +
geom_line(aes(y = Financial_Wealth.mean, color="Financial Wealth Mean"), size=1) +
geom_line(aes(y = Total_Wealth.mean, color="Total Wealth Mean"), size=1) +
scale_color_manual(values=c(
"Human Capital Mean"="red",
"Financial Wealth Mean"="goldenrod3",
"Total Wealth Mean"="dodgerblue"))

Related

Points not remaining clear over bar when using dodge

When I make my bar graph using this code, it becomes unclear which bar the points are supposed to be above. This seems to happen when I add the col function.
Any help would be great!
ggplot(Data_Task1, aes(Type, Percentage_Correct_WND, fill = Condition, col = Animal)) +
geom_bar(stat = "summary", col = "black", position = "dodge") +
geom_point(position = position_dodge(0.9)) +
labs(x = "", y = "% Correct Without No Digs") +
scale_fill_brewer(palette = "Blues") +
scale_colour_manual(values = c("#000000", "#FF9933", "#00FF33", "#FF0000", "#FFFF00", "#FF00FF")) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.line.x = element_line(colour = "black", size = 0.5),
axis.line.y = element_line(colour = "black", size = 0.5)
)
enter image description here
The issue is that ggplot2 does not know the column in your dataset it should use for the dodging when it comes to the point geom. For the bar geom, it's obvious because different bars are drawn with different fill colors at the same x axis value. For your point geom, ggplot2 is dodging based on the color aesthetic. If you want to force grouping or dodging based on a specific column, you should assign that column to the group= aesthetic.
Here's an example using mtcars. I'm forcing continuous factors to be discrete via as.factor() here.
library(ggplot2)
# plot with incorrect dodging
ggplot(mtcars, aes(x=as.factor(carb), y=mpg, fill=as.factor(cyl), color=as.factor(gear))) +
geom_bar(position="dodge", stat="summary", col='black') +
geom_point(position=position_dodge(0.9), size=3) +
theme_classic()
The bars are dodging based on as.factor(cyl), assigned to the fill aesthetic, but the points are dodging based on as.factor(gear), assigned to the color aesthetic. We override the color aesthetic in the geom_bar() command (as OP did) by defining col='black'.
The solution is to force the points to be grouped (and therefore dodged) based on the same column used for the fill aesthetic, so mapping is group=as.factor(cyl).
ggplot(mtcars, aes(x=as.factor(carb), y=mpg, fill=as.factor(cyl), color=as.factor(gear))) +
geom_bar(position="dodge", stat="summary", col='black') +
geom_point(aes(group=as.factor(cyl)), position=position_dodge(0.9), size=3) +
theme_classic()
Applied to OP's case, the dodging should work with this code:
ggplot(Data_Task1, aes(Type, Percentage_Correct_WND, fill = Condition, col = Animal)) +
geom_bar(stat = "summary", col = "black", position = "dodge") +
# adjust group here to Condition (same as fill)
geom_point(aes(group = Condition)), position = position_dodge(0.9)) +
labs(x = "", y = "% Correct Without No Digs") +
scale_fill_brewer(palette = "Blues") +
scale_colour_manual(values = c("#000000", "#FF9933", "#00FF33", "#FF0000", "#FFFF00", "#FF00FF")) +
theme(
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.line.x = element_line(colour = "black", size = 0.5),
axis.line.y = element_line(colour = "black", size = 0.5)
)

How do I get custom colors AND have my legend have the correct labels (ggplot)?

I have a lovely stacked bar chart (see attached image).carnival colors
I used the following code to create this masterpiece:
ggplot(data=data, aes(x=docket, y=Approved, fill=EMH_Cat)) + geom_bar(stat="identity", position="stack") +
labs(title="Type of Study by Project", x="Project", y="Number of Studies") +
theme(plot.title = element_text(size=30,hjust = 0.5)) +
theme(panel.background = element_rect(fill = 'white')) +
theme(axis.line = element_line(color="black", size = 0.5)) +
theme(axis.text.x= element_text(size=8,color = "black")) +
theme(axis.text.y = element_text(size=12, color = "black")) +
scale_y_continuous(breaks=seq(0,60,10)) + scale_x_discrete(labels=c("Bartletts\nFerry","Cabin\nCreek","Claytor","Emeryville","Green\nIsland","Jackson","Jennings\nRandolph","Keowee\nand\nToxaway","Loup\nCanal","Mahoning","Martin","Mason","Monadnock","Old\nHarbor","Oswegatchie\nRiver","Otter\nCreek","Salina","Tomahawk","Vanceboro","Wallowa\nFalls","Wells","Williams","York\nHaven")) +
theme(axis.title.x = element_text(face="bold", size=12,vjust=-0.5,hjust=0.5)) +
theme(axis.title.y = element_text(face="bold", size=12,vjust=2,hjust=.5)) +
scale_fill_discrete(name = "Categories", labels = c("Biota and Biodiversity","Connectivity and Fragmentation","Cultural Resources", "Geomorphology","Infrastructure and Design", "Landcover","Recreation", "Water Quality","Water Quantity")) +
theme(panel.border = element_rect(colour = "black", fill=NA),legend.box.background = element_rect(colour = "black"), legend.background = element_rect(linetype = "solid", colour = "black")) +
theme(legend.title.align=0.5) + theme(legend.position = c(0.09,0.7))
I used the read.csv function. My data looks like:snippet
The legend isn't in a fab place, but I can and will attend to that. However, the issue comes in when I try to add this little guy:
scale_fill_manual(values=c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000", "#a03223", "#9e07f5","#2abdda", "#306ebe"))
These were the colors that were in a certain publication and so I need them to be in this chart as well. If I run the first code chunk and then add this guy at the bottom I get an error, Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.
It looks like this:argh
I get why R does this, I just don't know how to fix it. I want to have my colors and eat the legend, too. HELP!!
You have scale_fill_discrete with name and labels arguments. You need to give those exact same arguments to your scale_fill_manual instead, along with your values. Delete your scale_fill_discrete line and add
scale_fill_manual(
name = "Categories",
labels = c("Biota and Biodiversity", "Connectivity and Fragmentation",
"Cultural Resources", "Geomorphology", "Infrastructure and Design",
"Landcover","Recreation", "Water Quality","Water Quantity")
values = c("#1e7640", "#de772d", "#e815dd", "#7f6c00", "#000000",
"#a03223", "#9e07f5","#2abdda", "#306ebe")
)

In ggplot2 how can I scale the legend when using two graph types?

I'm using ggplot2 with both + geom_line() + geom_point(). I have the colors/shapes worked out, but I can't scale the legend appropriately. If I do nothing it's tiny, and if I enlarge it, the color blocks the shape.
For example:
You can see that the shapes and colors are both in the legend, but the shapes are being drawn over by the colors. I would like to have shapes of the appropriate color drawn in the legend, but can't figure out how to do it.
My plot is being drown as follows:
ggplot(data=melted, aes(x=gene, y=value, colour=variable, shape=variable, group = variable, stroke=3, reorder(gene, value)))
+ theme_solarized()
+ scale_colour_solarized("blue")
+ geom_line()
+ geom_point()
+ theme(axis.text.x = element_text(angle = 90, hjust = 1), plot.title = element_text(size=16, face="bold"), legend.title=element_blank(), legend.text=element_text(size=20))
+ ggtitle('Signiture Profiles')
+ labs(x="Gene", y=expression(paste("Expression"), title="Expression"))
+ scale_colour_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c("#ff420e","#89da59","#89da59","#89da59","#376467","#376467","#376467","#00293c","#00293c","#00293c"))
+ scale_shape_manual(name = "Virus / Time", labels = c("Mock", "ACali09_day1", "ACali09_day3", "ACali09_day8", "AShng113_day1", "AShng113_day3", "AShng113_day8", "AChkShng113_day1", "AChkShng113_day3", "AChkShng113_day8"), values = c(0,1,2,3,1,2,3,1,2,3))
+ guides(colour = guide_legend(override.aes = list(size=12)))
Here is some example data as requested:Example Data
Thanks in advance for any help you can provide.
You could perhaps rethink how you are differentiating your variables.
You could do something like the following. Note the changes in the first line, where I have separated the component parts of variable rather than setting colours and shapes via your scale statements. (I haven't got your theme, so I left that out).
ggplot(data=melted, aes(x=gene,
y=value,
colour=gsub("_.*","",variable),
shape=gsub(".*_","",variable),
group = variable,
stroke=3,
reorder(gene, value))) +
geom_line() +
geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
plot.title = element_text(size=16, face="bold"),
legend.title=element_blank(),
legend.text=element_text(size=20)) +
ggtitle('Signiture Profiles') +
labs(x="Gene", y=expression(paste("Expression"), title="Expression")) +
guides(shape = guide_legend(override.aes = list(size=5)),
colour = guide_legend(override.aes = list(size=5)))

Add secondary axis ggplot - line chart with two variables - one data frame

I used ggplot to make a like graph with two variables, but I need to add a secondary y-axis and assign it to one of the variables ("volt").
I also would like to specify the range of the secondary y-axis (upper and lower limit), as well as the breaks - as I did for the y-main-axis.
My two variables are "Sr" and "volt".
I don't want to use different dataframes and then merge the graphs.
Do any of you know how to do it?
Oh, I must add that I am an absolute beginner!
Thanks,
Pedro
ggplot(data = k, aes(x = Dist)) +
geom_line(aes(y = Sr), colour="blue") +
geom_line(aes(y = volt), colour = "grey") +
xlab(bquote('Distance-um')) +
ylab(bquote('Sr87Sr86')) +
geom_point(aes(y = Sr), colour="black", size=2) +
geom_point(aes(y = volt), colour="grey", size=2) +
theme(axis.title.x = element_text(colour="black",size=10,face="bold"),
axis.title.y = element_text(colour="black",size=10,face="bold"),
axis.text.x = element_text(colour="black",size=8, face="plain"),
axis.text.y = element_text(colour="black",size=8, face="plain")) +
theme(panel.background = element_rect(fill = "white")) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black", fill="transparent")) +
theme(plot.title = element_text(lineheight=.8, size=10, face="bold")) +
geom_hline(aes(yintercept=0.7061), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.7078), colour="black", linetype="dotted") +
geom_hline(aes(yintercept=0.70467), colour="black", linetype="dotted") +
scale_x_continuous(limits=c(-0.01, 1000), breaks=c(0, 250, 500, 750, 1000))+
scale_y_continuous(limits=c(0.7039, 0.7101), breaks=c(0.7040, 0.7050,
0.7060, 0.7070, 0.7080, 0.7090)) +
theme(plot.margin = unit(c(.25,.25,.0,.0), "cm"))
First, I would like to mention that two axis is not the best idea.
Having said that, if you still want two axis, you have to scale one of your variables (volt in this case).
Dist<-seq(1,10)
Sr<-c(0.704, 0.705, 0.706, 0.707, 0.708, 0.704, 0.705, 0.706, 0.707, 0.708)
volt<-c(3,5,10,8,12,4,11,3,14,22)
k<-data.frame(Dist,Sr,volt)
k$volt<-k$volt/10
Now, fixing the data makes things easier for plotting, just melt your variables
library(reshape)
k_melt<-melt(k,id="Dist")
And plotting. With sec_axis you can create the second axis and rescale again the values
ggplot(k_melt, aes(x=Dist, y=value, fill=variable, colour=variable))+
geom_line(stat='identity', size=0.5)+
geom_point(stat='identity', size=2)+scale_color_manual(values=c("blue", "grey")) +
scale_y_continuous("SR", sec.axis = sec_axis(~ . *10, name = "Volt"))`
NOTE: You can add your theme and geom_hline to this code. they don't work for the simulated data I created

Error: no stat called StatHline

I have a data frame as follows:
variable=c("D","D","C","C","C","A","B","B","B","B")
value=c(80,100,70,68,65,45,33,31,36,32)
Count=as.integer(c(5,10,4,5,2,7,3,5,6,2))
mean=c(93.3,93.3,68.2,68.2,68.2,45,33.4,33.4,33.4,33.4)
sumVarVal=data.frame(variable=variable,value=value,Count=Count,mean=mean)
I can make a nice plot (where the size of the square corresponds to the count of observations with that particular x-value and y-value), as shown below:
library(ggplot2)
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90",
size=.5,
colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade")
However, I used to have an additional piece of code (at the end of the syntax) that allowed me to superimpose a horizontal bar on each of the four topics, indicating the mean percentage grade. Those values are stored in df$mean. Here is the code I was using:
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade") +
geom_errorbar(stat = "hline", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))
With version 1.0.1, this gives:
With version 2.0.0, it now leads to an error:
Error: no stat called StatHline.
I know this may be connected to recent upgrades in ggplot2. I have seen other recent comments about it (geom_errorbar - "No stat called StatHline"). However, due to my code surrounding the use of stat="hline", when I tried some of these suggestions, I was not able to get my code to work either. Perhaps there is something I do not understand about my original code that is preventing me from being able to update this issue?
EDIT: I have taken into account some of the suggestions, and am currently using this code:
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade") +
geom_errorbar(stat = "summary", fun.y = "mean", width=0.6, colour = "blue", size = 1, aes(ymax=..y..,ymin=..y.., yintercept = mean))
This gives me an output that looks like this:
It seems that some of the mean blue lines are not lining up to their values, as given originally in the mean vector. For instance, for variable "D", it should have a mean value of 93.3, but the blue horizontal line seems to be displayed at a value of 90.0. The effect is even more dramatic in my real code (not this MWE). Any ideas what might be causing this discrepancy?
stat_hline got removed in ggplot2 2.0.0, but never fear; it wasn't really necessary anyway. If you remove the stat argument entirely, it will default to identity, which is fine. (summary can work, too, if you prefer.) You need to change the aes mapping, though, changing yintercept to y to account for the new stat.
All together,
ggplot(sumVarVal, aes(variable, value)) +
geom_point(aes(size = Count), pch=15) +
guides(fill=guide_legend(title="New")) +
theme(legend.background = element_rect(fill="gray90", size=.5, colour = "black"),
legend.text=element_text(size=rel(1.3)),
legend.title=element_text(size=rel(1.3), face="plain"),
legend.position="bottom",
axis.text = element_text(size=rel(1.3)),
axis.title = element_text(size = rel(1.3))) +
labs(x="Learning Outcome", y = "Percentage Grade") +
geom_errorbar(width=0.6, colour = "blue", size = 1, aes(ymax=..y.., ymin=..y.., y = mean))
produces

Resources