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
Related
I would like to plot the data by subject but adding the errorbar of the total mean and se. I mean, not an error bar for each subject. I've tried geom_errorbar and stat_summary but still failed to get my ideal plot (see the figure I drew).
and here is the code I used to draw this figure (the errorbars are added by hand).
ggplot(ASD, aes(x=period, y=meanF0, group=subject, color=group)) +
geom_line(aes(color=group, size=group)) +
scale_size_manual(values=c(.6, .6, .6, .6)) +
theme_light()+
xlab("Period")+
ylab("F0 (Hz)")+
ggtitle("Mean F0 Adjustment (ASD Group)") +
geom_point()+
scale_color_manual(values=c("red")) +
theme(plot.title = element_text(size=14.5, face="bold", hjust = 0.5, family = "serif"),
axis.title.y= element_text(size=12, face = "bold", family = "serif"),
axis.title.x= element_text(size=12, face = "bold", family = "serif"),
axis.text.x = element_text(size=11, face="bold", family = "serif"),
axis.text.y = element_text(size=11, face="bold", family = "serif"))+
theme(legend.position = "none")+
geom_hline(yintercept=112.8, linetype="dashed",
color = "dark grey", size=.7)
Anyone could help? Thank you very much!!!
Use annotate to add the error bars. I don't have your data, so I created my own. You're going to need the confidence interval and the average for each group. My average-by-group values and confidence interval-by-group are stored in df4$meanV and df4$ci. You can replace these with your variable names. In annotate, you'll include the data frame in the call like you would in base R plots. Like base R, you can just use raw values, as well. Multiple values can be joined with c(). As in y = c(12, 10). If you have any questions, just let me know.
ggplot(df2, aes(x = condition, y = value,
color = subject, group = subject)) +
geom_line() + geom_point() +
annotate("errorbar",
x = df4$condition
ymin = df4$meanV - df4$ci,
ymax = df4$meanV + df4$ci,
width = .2) +
annotate("point",
x = df4$condition,
y = df4$meanV) +
ylim(min(df2$value), max(df2$value))
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"))
I have the formula below:
ggplot(Errortrialsmodifyoriginal, aes(x = Target, y = Absolutefirststoperror, color = as.character(Type), shape = as.character(Type))) +
geom_point(shape=16)+
geom_point(data=Errortrialoriginal,shape=19,size = 4,mapping=aes(x=Target, y=Absolutefirststoperror)) +
geom_line(data=Errortrialoriginal,aes(group=Type,linetype=Type),size=2,) +
scale_color_manual(name = "Condition", values = c("red","green","blue","red","green","blue")) +
scale_linetype_manual(name = "Condition",values = c("dashed","dashed","dashed","solid","solid","solid")) +
geom_errorbar(data=Errortrialoriginal,mapping=aes(x=Target, ymin=Absolutefirststoperror-SE,ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() + guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"), axis.title.y = element_text(size=14, face="bold"),legend.text=element_text(size=14),title=element_text(size=14,face="bold"), plot.title = element_text(hjust = 0.5), legend.title = element_text(size=14,face="bold"), axis.text.x=element_text(size=14),axis.text.y=element_text(size=14),panel.grid.major = element_blank(), panel.grid.minor = element_blank())
Which produces the graph:
How can I change my command to ensure that the dashed and solid lines are shown in the figure legend; because at the moment, the figure legend suggests that all the lines are solid, even though they are not?
I would be grateful for any advice!
To my opinion, the legend is correctly displayed but you can't see it because you have big points front of the linetype. You should increase the legend box to see it.
Here an example with this dummy example:
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)
library(ggplot2)
ggplot(my_data, aes(x = dose, y = length, color = supp, linetype = supp))+
geom_line()+
geom_point(size = 4)+
theme(legend.key.size = unit(3,"line"))
So, with your code, you can do something like that:
library(ggplot2)
ggplot(Errortrialsmodifyoriginal,
aes(x = Target,
y = Absolutefirststoperror,
color = Type)) +
geom_point()+
geom_line(data=Errortrialoriginal,
aes(group=Type,
linetype=Type)) +
scale_color_manual(name = "Condition", values = rep(c("red","green","blue"),2)) +
scale_linetype_manual(name = "Condition",values = rep(c("dashed","solid"),each =3)) +
geom_errorbar(data=Errortrialoriginal,
mapping=aes(x=Target,
ymin=Absolutefirststoperror-SE,
ymax=Absolutefirststoperror+SE),size=0.5) +
theme_bw() +
guides(color = guide_legend("Condition"), shape = guide_legend("Condition"), linetype = guide_legend("Condition")) +
labs(x = "Target distance (vm)", y = "Absolute error in stop location (vm)") +
theme(axis.title.x = element_text(size=14, face="bold"),
axis.title.y = element_text(size=14, face="bold"),
legend.text=element_text(size=14),
title=element_text(size=14,face="bold"),
plot.title = element_text(hjust = 0.5),
legend.title = element_text(size=14,face="bold"),
axis.text.x=element_text(size=14),
axis.text.y=element_text(size=14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.key.size = unit(3,"line"))
Does it answer your question ?
If not, please consider providing a reproducible example of your dataset (see: How to make a great R reproducible example)
I've look up so many sample code snippets online but for the life of me I cannot figure out what I'm doing wrong. I've included the r code below that gives me this chart:
It's generally working, except:
I can't get it to show %s instead of counts (for the factor variable "doctorate"), and
The colors for "group" show up in the legend, but not in the bars themselves.
pg_doc <- ztemp.pg %>%
ggplot(aes(doctorate, group = group)) +
geom_bar(aes(y = ..prop.., fill = group), stat="count") +
geom_bar(position = position_dodge(preserve = "single")) +
scale_fill_manual(values=c("grey45","goldenrod2")) +
theme(legend.position = "bottom",
legend.title=element_blank(),
text = element_text(color="black"), #for x & y axis text labels
axis.text.x = element_text(color="black", size=10, hjust=1),
axis.text.y = element_text(color="black"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.title=element_text(hjust=.5))+
ggtitle("Highest degrees attained by leadership")+
scale_x_discrete(labels= c("Other", "Doctorate"))
pg_doc
Just write one geom_bar. You can also move fill argument to the aes command in ggplotfunction. To show percentages, you have to calculate them apriori and plot. A simple reproducible example will provide more clarity.
pg_doc <- ztemp.pg %>%
ggplot(aes(doctorate, group = group)) +
geom_bar(aes(y = ..prop.., fill = group), stat="count",position = position_dodge(preserve = "single")) +
scale_fill_manual(values=c("grey45","goldenrod2")) +
theme(legend.position = "bottom",
legend.title=element_blank(),
text = element_text(color="black"), #for x & y axis text labels
axis.text.x = element_text(color="black", size=10, hjust=1),
axis.text.y = element_text(color="black"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
plot.title=element_text(hjust=.5))+
ggtitle("Highest degrees attained by leadership")+
scale_x_discrete(labels= c("Other", "Doctorate"))
pg_doc
I made ton of figures with ggplot 0.8.9 (which is what I'm still running). Now I need to modify these figures to include legends. I am running across all sorts of problems that are hard to solve because I am getting really confused about theme and opts and many SO answers that apply to later versions.
At this point, it seems like I need to update ggplot2 and rewrite all of my code just so I can have legends on my figures. Is this true? I've read the ggplot2 transition guide, it makes it seem true.
Here's what the old code looks like (does not produce a legend): And here is the data for the sake of reproducibility: mean10v2 and stderr10.
me10<-read.table("mean10v2.txt", header=TRUE)
se10<-read.table("stderr10.txt", header=TRUE)
ggplot() +
geom_ribbon(aes(x = me10[me10$trt=="CC", "tu"], ymin=(me10[me10$trt=="CC", "biomassA"]-
se10[se10$trt=="CC", "biomassA"]), ymax=(me10[me10$trt=="CC",
"biomassA"]+se10[se10$trt=="CC", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="CC", "tu"], y=me10[me10$trt=="CC", "biomassA"]), size=1)+
geom_ribbon(aes(x = me10[me10$trt=="PF", "tu"], ymin=(me10[me10$trt=="PF", "biomassA"]-
se10[se10$trt=="PF", "biomassA"]), ymax=(me10[me10$trt=="PF",
"biomassA"]+se10[se10$trt=="PF", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="PF", "tu"], y=me10[me10$trt=="PF", "biomassA"]),
colour="red2", linetype="dashed", size=1) +
geom_ribbon(aes(x = me10[me10$trt=="P", "tu"], ymin=(me10[me10$trt=="P", "biomassA"]-
se10[se10$trt=="P", "biomassA"]), ymax=(me10[me10$trt=="P",
"biomassA"]+se10[se10$trt=="P", "biomassA"])), alpha=0.25) +
geom_line(aes(me10[me10$trt=="P", "tu"], y=me10[me10$trt=="P", "biomassA"]),
colour="blue3", linetype="dotted", size=1) +
opts(panel.grid.minor = theme_blank()) +
opts(panel.grid.major = theme_blank()) +
opts(panel.background = theme_blank()) +
opts(axis.line = theme_segment()) +
opts(legend.position=c(.5,.5)) +
opts(axis.title.x = theme_text(size=12,vjust=-0.5)) +
opts(axis.title.y = theme_text(size=12,angle=90)) +
opts(axis.text.x = theme_text(colour="black", size=16)) +
opts(axis.text.y = theme_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size
= 9) +
ylab("") +
xlab("") +
ylim(0,2200)
Updating the theme parts is actually quite simple. You really just need to change opts() to theme() and replace theme_* with element_*. Some other names have changed, like you'll use element_line instead of theme_segment.
But more generally, you're using ggplot all wrong:
my_df <- me10[,c('trt','tu','biomassA')]
my_se <- setNames(se10[,c('trt','tu','biomassA')],c('trt','tu','se'))
my_df <- merge(my_df,my_se)
ggplot(data = my_df,aes(x = tu,y = biomassA)) +
geom_ribbon(aes(group = trt,ymin = biomassA - se,ymax = biomassA + se),alpha = 0.25) +
geom_line(aes(group = trt,linetype = trt,colour = trt)) +
labs(x = "",y = "") +
ylim(0,2200) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(),
legend.position=c(.5,.5),
axis.title.x = element_text(size=12,vjust=-0.5),
axis.title.y = element_text(size=12,angle=90),
axis.text.x = element_text(colour="black", size=16),
axis.text.y = element_text(colour="black", size=16)) +
annotate("text", x = -Inf, y = Inf, label = "a", face="bold", hjust = -5, vjust=2, size = 9)
Notice how much cleaner that is, and putting the data into an appropriate form only took three lines. Also note that there is absolutely no need to keep repeating the opts() or theme() calls for every....single....thing...you....set.
And then if you want to choose specific colors/linetypes for each group, you do that using the scale functions, not by setting them individually:
+ scale_colour_manual(values = c('black','red2','blue3')) +
scale_linetype_manual(values = c('solid','dashed','dotted'))