I found an issue with ggplot2::geom_text() when I attempt to highlight my data with labels. My MWE illustrates the situation. After I define the plot I add the theme() from phase#1 which colors the panel and plot backgrounds. The red/green is for emphasis. When the labels are added in phase#2, the plot.background goes to white and the panel background to the default grey. Is this something I'm doing, is this an undocumented feature of ggplot2, or is this a bug?
require(ggplot2)
SESnow <- data.frame(Year=c(1978,1988,1995,2000,2013,2017),
Snow=c(355.9,322.1,329.1,303.6,318.5,304.0))
p <- ggplot(SESnow, aes(x=Year, y=Snow, fill=Snow)) +
geom_col(width=1) +
scale_fill_gradient(low="blue", high="red", limits=c(0,400)) +
theme(axis.title.y=element_text(angle=0)) +
ggtitle("Yearly Total Snowfall (inch)") +
labs(subtitle = "Copper City 2019",
caption="Source: Keweenaw County",
x="Snow Season") +
theme(legend.position="none")
#phase#1
p + theme( panel.background = element_rect(fill = "green",
colour = "black",
size = 0.5, linetype = "solid"),
plot.background = element_rect(fill = "blue",
colour = "black",
size = 0.5, linetype = "solid"),
axis.text.x = element_text(colour="grey20",size=11, vjust=1,
margin=margin(t=0,b=0),face="bold"),
axis.text.y = element_text(colour="grey20",size=11, hjust=1,
margin=margin(t=10,b=10),face="bold") )
#phase#2
p + geom_text(data=SESnow, aes(label = Snow, fill=NULL ), y = SESnow$Snow + 20.0,
label=format(SESnow$Snow, digits=2), size=3, fontface="bold",
color="black")
Also note that if you run phase#1 after phase#2 the labels disappear, so this feature is consistent. How do I get a plot with labels and colored background?
The answer is simple. You are generating two plots instead of one. You need to store the plot in p if you want to use the same plot and modify it later.
#phase#1
p <- p + theme( panel.background = element_rect(fill = "green",
colour = "black",
size = 0.5, linetype = "solid"),
plot.background = element_rect(fill = "blue",
colour = "black",
size = 0.5, linetype = "solid"),
axis.text.x = element_text(colour="grey20",size=11, vjust=1,
margin=margin(t=0,b=0),face="bold"),
axis.text.y = element_text(colour="grey20",size=11, hjust=1,
margin=margin(t=10,b=10),face="bold") )
#phase#2
p + geom_text(data=SESnow, aes(label = Snow, fill=NULL ), y = SESnow$Snow + 20.0,
label=format(SESnow$Snow, digits=2), size=3, fontface="bold",
color="black")
Assign the value to p before you use it again. This will solve the issue.
Edit: I am attaching the graph. I guess this is what you wanted.
Related
I made one graph with 'two line' y-axis title using the code ylab(expression(paste()
Genotype <- c("CV1","CV2","CV1","CV2")
Category<- c("GN","GN","AGW","AGW")
mean <- c(1.47,1.66,0.98,0.93)
se<- c(0.10,0.20,0.03,0.06)
DataA<- data.frame(Genotype,Category,mean,se)
DataA
ggplot(data=DataA, aes(x=Genotype, y=mean, fill=Category))+
geom_bar(stat="identity",position="dodge", width = 0.7) +
geom_errorbar(aes(ymin= mean-se, ymax=mean + se), position=position_dodge(0.7),
width=0.2, size=1) +
scale_fill_manual(values= c ("Dark gray", "Cadetblue")) +
scale_y_continuous(breaks = seq(0,2,0.2), labels = scales::percent, limits = c(0,2)) +
geom_hline(yintercept=1, linetype="dashed", color = "Dark red", size=1.5) +
xlab("Cultivar") +
ylab(expression(paste("Value of thinning treatment \n relative to unthinning treatment"))) +
theme(axis.title = element_text (face = "plain", size = 15, color = "black"),
axis.text.x = element_text(size= 15),
axis.text.y = element_text(size= 15),
axis.line = element_line(size = 0.5, colour = "black"))+
windows(width=7, height=5)
The y-axis title was divided into two lines, but the whole title was not fitted inside the graph. Even though I adjust the width of graph size, it was not applied to the axis title.
Could you tell me how to fit the whole y-axis title inside the graph well, maintaining the size of axis title (i.e. font size= 15)?
Thanks,
One way would be to adjust the margins giving more space to the left.
library(ggplot2)
ggplot(data=DataA, aes(x=Genotype, y=mean, fill=Category))+
geom_bar(stat="identity",position="dodge", width = 0.7) +
geom_errorbar(aes(ymin= mean-se, ymax=mean + se), position=position_dodge(0.7),
width=0.2, size=1) +
scale_fill_manual(values= c ("Dark gray", "Cadetblue")) +
scale_y_continuous(breaks = seq(0,2,0.2), labels = scales::percent, limits = c(0,2)) +
geom_hline(yintercept=1, linetype="dashed", color = "Dark red", size=1.5) +
xlab("Cultivar") +
ylab(expression(paste("Value of thinning treatment \n relative to unthinning treatment"))) +
theme(axis.title = element_text (face = "plain", size = 15, color = "black"),
axis.text.x = element_text(size= 15),
axis.text.y = element_text(size= 15),
axis.line = element_line(size = 0.5, colour = "black")) +
theme(plot.margin=unit(c(1,0.5,0.5,1.2),"cm"))
I'm using ggplot2 in R to make an x-y graph and I was wondering how I can make the grid line y=0 to be black, while leaving the other grid lines grey
here is what I have so far
ggplot(data = subset(data, org %in% c('T','P','TP','PP')), mapping=aes(x=AA, y=v, color=org))+
geom_point()+
stat_smooth(method='lm', formula = y~log(x), se=FALSE)+
scale_y_continuous( name=expression(paste('%',Delta,V)))+
coord_cartesian(ylim = c(0.01, -0.035))+
scale_x_continuous(name='#A')+
ggtitle('Changes in Volume')+
theme(plot.title = element_text(hjust = 0.5),
axis.line.x = element_line(color="black", size = 2),
panel.grid.major = element_line(color = "grey"),
panel.grid.minor = element_line(color = "grey"),
panel.background = element_rect(colour = "black", size=4, fill=NA))
I would like to emphasize the fact that some of my data is above the y=0 and the other part is below.
thanks
" + geom_hline(yintercept = 0, color = "black") – Claus Wilke "
I need my charts to have a dark gray theme to match a presentation style. I also need them to be fixed height, but the widths may vary depending on the length of the labels on the y-axis. When I try to export or save, there are always white side bars in the .jpg or .png file.
Here's some sample code that I use to create the chart (there are some extra theming controls in here that are superfluous to the simplified example, but the resulting chart is basically what I am generating):
library(ggplot2)
bar.font <- 8
title <- "Example"
l_labs <- c("")
x_labs <- c("A","B","C")
ests <- c(.5,.3,.2)
nerrs <- c(.05, .05, .05)
perrs <- nerrs
barchart.data <- data.frame(l_labs, x_labs, ests, nerrs, perrs)
p <- ggplot(barchart.data, aes(x=x_labs, y=ests*100)) +
geom_bar(stat="identity", color="#808080", position=position_dodge(), width=0.85, fill="#808080") +
geom_text(aes(y=ests*100+perrs*100+1.5, label=sprintf("%1.1f%%", 100*ests)), vjust=0.5, hjust=0, size=bar.font, color="white") +
geom_errorbar(aes(ymin=ests*100-nerrs*100, ymax=ests*100+perrs*100), width=.2, position=position_dodge(.9), color="white", size=0.25) +
labs(title=title, x="", y = "") + theme_classic() +
scale_y_continuous(expand = c(0,0),limits = c(0,115), breaks=c(0, 20, 40, 60, 80, 100)) +
theme(legend.position="none", legend.text = element_text(color = "white")) +
theme(title = element_text(size=18, colour = "white")) +
theme(axis.text = element_text(size=20, color = "white"), axis.line = element_line(color = "white")) +
theme(axis.text.x = element_text(margin=margin(9,0,0,0)),axis.text.y = element_text(margin=margin(0,9,0,0))) +
theme(axis.title = element_text(size=20, color = "white")) +
theme(axis.title.x = element_text(margin = margin(10,0,0,0))) +
theme(axis.ticks = element_line(colour = 'white', size = .5)) +
coord_flip() +
theme(aspect.ratio = 1) +
theme(panel.background = element_rect(fill = "#1e1e1e")) +
theme(legend.justification=c(1,0), legend.position=c(1,0)) +
theme(plot.background = element_rect(fill = "#1e1e1e", color = "#1e1e1e")) +
theme(panel.grid.major.x = element_line(colour = "white",size=0.1, linetype = "dotted"))
ggsave("test.jpg", height=10, units="in")
And here is what the exported .jpg looks like. I cannot specify an exact width because I do not know what it will be for each chart as the widths vary. Thanks for any guidance.
You can set the background color to whatever value you like:
ggsave("test.jpg", height=10, units="in", bg = "#1e1e1e")
This takes care of the white bars.
It may be a bit confusing that the bg option is not mentioned in the ggsave() documentation. That's because it's part of the graphics device, here jpeg(). It is mentioned in the jpeg() documentation.
The reason why there are white bars in the first place is discussed in depth in this post.
I am trying to change one of the lines in my ggplot from dash to dotted but I fail, the function scale_linetype_manual doesn't do the work. Can anyone help me to solve this problem?
ggplot(d, aes(a,value)) +
geom_line(aes(color = series), size=2)+
scale_y_continuous(breaks=seq(-2.5, 2.5, 2.5)) +
coord_cartesian(ylim=c(-2.5, 2.5))+
scale_x_continuous(breaks=seq(-200, 2000, 1000)) +
scale_color_manual(values=c("#E69F00","#56B4E9", "#56B4E9")) +
scale_linetype_manual(values=c("twodash", "dotted", "dotted")) +
theme(legend.direction = 'vertical',
legend.position = 'right',
legend.key = element_rect(size = 7),
legend.key.size = unit(3, 'lines'))+
theme(panel.grid.major = element_blank(), text = element_text(size=30),
panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour =
"black"))+
geom_vline(xintercept=c(0), linetype="dotted", size=1.5)+
geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),
color="gray55",
alpha=0.4,
inherit.aes = FALSE)+
labs(x = "time [ms]",
y = "Amplitude [µV]",
color = "")
The 'problem' with your plot was that you didn't map the aesthetic linetyppe
to a variable. Hence, your call to scale_linetype_manual had no effect. You should change your code to be
ggplot(d, aes(a,value)) +
geom_line(aes(color = series, linetype = series), size = 2) +
...
This is question linked to previous question (Adjusting legend.title ,legend.text and legend color in ggplot2). I am having issue to change color of the geom points (Run and Walk Segmentation) in the plot. Can anyone please help me in this ? Is there any other way that i can have more better visualizations for the segmentation ? Thanks
er<- ggmap(sq_map2) +
geom_point(data = sisquoc, size = 8, aes(fill = Segmentation)) +
geom_line(data = sisquoc, size = 3, aes(color =SpeedMeterPerSecond)) +
geom_text(data = sisquoc, aes(label = paste(" ",
as.character(Location_ids),
sep="")),
angle = 60, hjust = 0, color = "sienna4",size = 6 )
gg<- er + labs(x ="Longitude", y = "Latitude") +
theme(axis.title = element_text(size=20),
panel.background = element_rect(fill = "white",size = 0.5, linetype =
"dotted"),
panel.grid.major = element_line(size = 0.5, linetype =
'dotted',colour
= "black"),
panel.grid.minor = element_line(size = 0.5, linetype =
'dotted',colour
= "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
axis.text.y = element_text(size=18),
axis.text.x = element_text(size=18))
gg + theme(legend.position="right",
legend.title = element_text(colour="Black", size=18),
legend.text = element_text(colour="black", size = 15),
legend.background = element_rect(fill="grey90",
size=0.5, linetype="solid",
colour ="black")) +
scale_color_continuous(name="Speed (m/s)\n")
I assume that you want to change the colour of the points in the plot.
try + scale_fill_manual(values = c("Run" = "black","Walk" = "grey"))
For geom_point to make use of aes(fill=...) you have to select shapes that can take fill values in addition to colour values, otherwise geom_point takes aes(colour=...). Fill is the appropriate aes to use here since you are already making use of aes(colour=...) for geom_line.
See possible shapes 21 to 25 that take fill values here
Try:
ggmap(sq_map2) +
geom_point(data = sisquoc, size = 8, aes(fill = Segmentation, shape = Segmentation) +
scale_shape_manual(values=c(21, 24))
You can further define fill values using e.g. scale_fill_manual(values=c("red", "blue"))