I would like to add a label containing multiple indices to a bar plot:
data<-as.data.frame(c("A"))
colnames(data)<-"A"
data$B<-5
ggplot(data, aes(x=A, y=B)) +
geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) +
annotate("text", x=1, y=2.5, label="some text")
I need to replace "some text" with "a1 a2 a3" where the numbers are in subscript. I tried the following but get errors:
annotate("text", x=1, y=0.4, parse=T, label=paste("a[1]","a[2]","a[3]",sep=" "))
The following does that:
ggplot(data, aes(x=A, y=B)) + geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) + annotate("text", x=1, y=2.5, label="~a[1]~a[2]~a[3]", parse=TRUE)
Related
I've produced a bar graph, however I want the data to be switched.
I want the blue area (the live cells) on the bottom and the dead cells on the top.
I tried reorder, reordering it by making it a factor but it all didn't work.
Who can help me?
Code I have now:
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Graph I have now:
library(forcats)
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=fct_shift(Type))) +
geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
Have you tried position_stack(reverse = TRUE)?
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) +
geom_bar(aes(fill = Type), stat="identity", position = position_stack(reverse = TRUE)), color="black") +
scale_fill_manual(values=c("lightblue","grey")) +
theme_bw() + theme(legend.position="right", axis.title.x = element_blank())
`install.packages("forcats")
b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill = forcats::fct_rev(Type))) +
geom_bar(stat="identity", position="stack", color="black") +
scale_fill_manual(values=c("lightblue","grey"))`
Forcats did the trick
I have the following code (see below). I want to add the mean value of the variable value as a label to the points in the chart. Any suggestions?
Thanks for your help! :)
df_1 <- data.frame(group=rep(c("A","B","C"),3),value=rnorm(9))
ggplot(df_1,aes(x=group,y=value)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
stat_summary(fun.y=mean, geom="point")
Add another stat_summary:
ggplot(df_1,aes(x=group,y=value)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
stat_summary(fun.y=mean, geom="point") +
stat_summary(fun.y=mean, geom="label", aes(label = round(..y.., 2)), hjust = -0.1)
Or geom_label:
ggplot(df_1,aes(x=group,y=value)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1) +
stat_summary(fun.y=mean, geom="point") +
geom_label(stat = 'summary', fun.y=mean, aes(label = round(..y.., 2)), nudge_x = 0.1, hjust = 0)
I am mapping aesthetic to a shape and created a manual shape scale. But the line connecting the points showing same color in the legend for two different lines.
p <- ggplot(data=data_read) + stat_summary(colour="blue", fun.y=mean,
geom="line", size=1.0, aes(x=factor(x), y=A, group=1), show.legend=T) +
stat_summary(fun.y=mean, geom="point", aes(x=factor(x), y=A, shape="A"),
size=5,col="black") +
stat_summary(colour="green", fun.y=mean, geom="line", size=1.0, aes(x=factor(x),
y=B, group=1), show.legend=T) +
stat_summary(fun.y=mean, geom="point", aes(x=factor(x), y=B, shape="B"),
size=3, col="black") +
scale_shape_manual("title", values=c("A"="\U0394", "B"="*"))
p + theme(legend.position = c(0.5,0.5),
legend.text = element_text(size = 15, colour = "black"),
legend.title = element_text(size = 15, colour = "black")) +
coord_cartesian(ylim = c(10,30), expand = FALSE) +
expand_limits(x=0, y=10)
My data file is here.
When I use geom_text on a ggplot, there is a conflict with the ggplot "fill" option.
Here is a clear example of the problem:
library(ggplot2)
a=ChickWeight
str(a)
xx=data.frame(level=levels(a$Chick),letter=1:50)
# a graph with the fill option alone
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
xlab("Chick") +
ylab("Weight")
# a graph with the geom_text option alone
x11();ggplot(a, aes(x=Chick, y=weight)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=450,label = letter)) +
xlab("Chick") +
ylab("Weight")
# a graph with the two option
x11();ggplot(a, aes(x=Chick, y=weight,fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")
If you only want the fill to affect the boxplot, move the aes() into the boxplot. Any aes() aesthetics in the ggplot() call itself will be propagated to all layers
ggplot(a, aes(x=Chick, y=weight)) + geom_boxplot(aes(fill=Diet), notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter)) +
xlab("Chick") +
ylab("Weight")
you can also disable the fill= aesthetic in the text layer with fill=NULL
ggplot(a, aes(x=Chick, y=weight, fill=Diet)) + geom_boxplot(notch=F) +
stat_summary(fun.y="mean", geom="point", shape=23, size=3, fill="white") +
geom_text(data=xx, aes(x=level,y=1750,label = letter, fill=NULL)) +
xlab("Chick") +
ylab("Weight")
I want to add a legend for the main diagonal and the regression line to the scatter plot.
What I have got now:
library(ggplot2)
df = data.frame(x = 1:10, y = 1:10)
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_smooth(method="lm", se=FALSE, formula=y~x, colour="blue", fill=NA, size=1.2) +
geom_abline(intercept=0, slope=1, size=1.2, colour="red") +
geom_text(aes(x=max(df[,1])/1.4, y=max(df[,2])/1.2, label=lm_eqn(df)), colour="blue", parse=TRUE) +
# doesn't work: scale_colour_manual("Lines", labels=c("Main Diagonal", "Regression"), values=c("red", "blue")) +
labs(x="X", y="Y")
use show_guide=TRUE e.g.
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_smooth(method="lm", se=FALSE, formula=y~x, colour="blue", fill=NA, size=1.2) +
geom_abline(aes(colour="red"),intercept=0, slope=1, size=1.2,show_guide=TRUE) +
geom_text(aes(x=max(df[,1])/1.4, y=max(df[,2])/1.2, label="lm_eqn(df)"), colour="blue", parse=TRUE) +
# doesn't work: scale_colour_manual("Lines", labels=c("Main Diagonal", "Regression"), values=c("red", "blue")) +
labs(x="X", y="Y") + opts(legend.position = 'left')
plus you can move legends about using things like+ opts(legend.position = 'left') to get it on the left. I suggest you look at the link provided by Tyler Rinker and also the following:
https://github.com/hadley/ggplot2/wiki/Legend-Attributes
Also no idea what lm_eqn ia so in my code i have surrounded it with "" so it will appear as it is written..
I could finally manage to create a legend for the regression and the diagonal line which is located in the bottom right corner and that makes sense:
p <- ggplot(df, aes(x, y)) +
geom_point(size=1.2) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0,0)) +
geom_abline(aes(colour="red"),intercept=0, slope=1, size=1.2, aes(colour="1"), show_guide=TRUE) + # new code
geom_smooth(method="lm", se=FALSE, formula=y~x, fill=NA, size=1.2, aes(colour="2"), show_guide=TRUE) + # new code
scale_colour_manual("Lines", labels=c("Diagonal", "Regression"), values=c("red", "blue")) +
opts(legend.position = c(0.85, 0.15)) # relative values, must be set individually