I am trying to annotate text in a ggplot2 plot combining greek letter with apostrophe and text. Till now I am not able to achieve the probem.
For now, I can write the following label, combining annotate and geom_text, here is the code:
ggplot(Lab_all_direct_shear)+
geom_rect(aes(xmin = 0, xmax = 20, ymin = 0, ymax = 120), color = NA, fill="grey", alpha = 0.05)+
geom_segment(linetype = 8,color="#666666",aes(x = 0, y = 0, xend = 55, yend = 88.649))+
geom_point(size=3,shape=21,fill ="#F8766D",aes(x=constitutive_normal_stress_15_kPa,y=constant_volume_shear_strenght_15_kPa))+
geom_point(size=3,shape=21,fill ="#00BA38",aes(x=constitutive_normal_stress_19_kPa,y=constant_volume_shear_strenght_19_kPa))+
geom_point(size=3,shape=21,fill ="#619CFF",aes(x=constitutive_normal_stress_25_kPa,y=constant_volume_shear_strenght_25_kPa))+
scale_x_continuous(limits = c(0,60),breaks=c(0,10,20,30,40,50,60),expand = c(0, 0)) +
scale_y_continuous(limits = c(0,120), breaks=c(0,10,20,30,40,50,60,70,80,90,100,110,120),expand = c(0, 0)) +
labs(x=expression(~sigma[n]~+~s~S[r]~(kPa)),y = "",title="") +
annotate("text", x = 54, y = 70, label = "= 58.2°",size=4)+
geom_text(x=50,y=70,label="phi",parse=TRUE)+
theme_bw() +
theme(plot.title = element_text(size=10, face="bold", hjust=0.5),
axis.title = element_text(size=10),
axis.title.x = element_text(angle=0, hjust=0.5),
axis.title.y = element_blank(),
axis.text = element_text(size=10,color="black"),
axis.text.x = element_text(size=10),
axis.text.y = element_blank(),
panel.grid.major = element_line(color= NA),
panel.grid.minor = element_line(color= NA),
plot.margin=unit(c(0.1,0.2,0.1,0.4), "cm"), #The four numbers are c(top,right,bottom,left)
legend.text = element_text(size=10),
legend.title = element_text(size=10),
legend.position = "none")
And the result in R ggplot2 package:
greek letter annotate without apostrophe
I have tried different combinations (declaring it as expression, paste, etc, but I am not able to solve it).
I would like to obtain a plot like this in excel, with the following annotation (phi with superscript apostrophe in combination with text):
excel plot with desired annotation
Any help will be highly appreciated!
Thanks in advance,
Raül
Here's a sample image:
data <- data.frame(x = 1:10, y = 1:10)
ggplot(data, aes(x, y))+
geom_point(color = "blue")+
geom_line(linetype = "dashed", color = "blue")+
annotate("text", x = 6, y = 7, label = "phi*' \\''==58^o",
parse = TRUE)+
theme_bw()
Related
I'm trying to plot a liine on the x axis which is basically a bunch of zeros and ones. Ones are green and zeros are red. When I try to do that, the color_scale_gradient of the ggplot basically colors on top of the line.
It looks like this
Where the line should be colored as follows:
colorbar is a vector of zeros and ones.
p <- ggplot(data1,aes(newx,newy, group = 1, colour=newy))+
geom_line(size=1.5, show.legend = FALSE)+
scale_colour_gradient(low="red2", high="green3") +
geom_line(data = colorFrame, aes(as.numeric(x)-5,as.numeric(ys), color = colorbar),size=3, show.legend = FALSE)+
xlim(0,1300)
p <- p +
theme(panel.background = element_blank(), axis.ticks.x = element_blank(),
axis.text.x = element_blank(), axis.line.y = element_line(colour = 'black'),
axis.ticks.y.left = element_line(colour = 'black')) +
scale_y_continuous(breaks = seq(0, 12, 1), limits = c(-1, 12), expand = c(0,0))
One solution would be to create two subplots and stitch them together. I use cowplot and theme_void here, but really the second plot below could look however you want it to.
p1 <- ggplot(df, aes(x,y, group = 1, colour=y)) +
geom_line(size=1.5, show.legend = FALSE) +
scale_colour_gradient(low="red2", high="green3") +
theme(panel.background = element_blank(),
axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.line.y = element_line(colour = 'black'),
axis.ticks.y.left = element_line(colour = 'black')) +
scale_y_continuous(breaks = seq(0, 12, 1), limits = c(-1, 12), expand = c(0,0)) +
labs(x = NULL)
p2 <- ggplot(df, aes(x, y = 0, colour=z)) +
geom_line(size=1.5, show.legend = FALSE) +
scale_colour_gradient(low="red2", high="green3") +
theme_void()
cowplot::plot_grid(p1, p2,
ncol = 1,
rel_heights = c(1, .05),
align = 'v')
Data
df <- data.frame(x = 1:50,
y = runif(50, 0, 12),
z = sample(c(0,1), 50, replace = TRUE))
This question already has answers here:
Wrap long axis labels via labeller=label_wrap in ggplot2
(4 answers)
Closed 2 years ago.
I know hjust is used for the x title axis, but how would I go about centering and multilining the x axis labels? Here is my plotting function:
gg_fun<-function(){
ggplot(tab,
aes(x = Var1, y = Percent)) +
#theme_light() +
theme(panel.background = element_rect(fill = NA),
axis.title.y=element_text(angle=0, vjust=0.5, face="bold"),
axis.title.x=element_blank(),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 12),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
#panel.grid.minor = element_line(colour = "dark gray"),
panel.grid.major.x = element_blank() ,
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y = element_line(size=.1, color="dark gray" ),
axis.line = element_line(size=.1, colour = "black"),
plot.background = element_rect(colour = "black",size = 1)) +
geom_bar(stat = "Identity", fill="#5596E6") + #"cornflower" blue
ggtitle(element_blank()) +
scale_y_continuous(expand = c(0, 0), breaks = round(seq(0, 1, by = .1), digits = 2),
labels = percent(round(seq(0, 1, by = .1), digits = 2), digits = 0),
limits = c(0,.6)) #+
#scale_x_discrete()
}
Here is an example graph it produces:
I am aware of n.dodge argument for scale_x_discrete(), but this is not what I am looking for. I also do not want to simply abbreviate using labels = abbreviate or specifying precisely as this is time consuming. I have also seen for example levels(birds$effect) <- gsub(" ", "\n", levels(birds$effect)), but this skips every line and makes some labels far too long. How would I go about centering the x label text as well as having it multiline to prevent overlap? Example of what I am going for:
You can use stringr::str_wrap as a labelling function in scale_x_discrete.
Let's take some sample data:
tab <- data.frame(Var1 = c("Video of presentation incl visuals",
"Video of presentation, written text and visuals",
"Written text, plus visuals",
"Other (please specify)"),
Percent = c(0.33, 0.34, 0.16, 0.17))
With your original function, this gives the following plot:
gg_fun()
But with the following modification:
gg_fun<-function(){
ggplot(tab,
aes(x = Var1, y = Percent)) +
#theme_light() +
theme(panel.background = element_rect(fill = NA),
axis.title.y=element_text(angle=0, vjust=0.5, face="bold"),
axis.title.x=element_blank(),
axis.text.y = element_text(size = 10),
axis.text.x = element_text(size = 12),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
#panel.grid.minor = element_line(colour = "dark gray"),
panel.grid.major.x = element_blank() ,
# explicitly set the horizontal lines (or they will disappear too)
panel.grid.major.y = element_line(size=.1, color="dark gray" ),
axis.line = element_line(size=.1, colour = "black"),
plot.background = element_rect(colour = "black",size = 1)) +
geom_bar(stat = "Identity", fill="#5596E6") + #"cornflower" blue
ggtitle(element_blank()) +
scale_y_continuous(expand = c(0, 0),
breaks = round(seq(0, 1, by = .1), digits = 2),
labels = scales::percent(round(seq(0, 1, by = .1),
digits = 2), digits = 0),
limits = c(0,.6)) +
scale_x_discrete(labels = function(x) stringr::str_wrap(x, width = 16))
}
We get:
gg_fun()
Typically, you have to manually place the newline character '\n' within your labels. However, someone wrote a function to do this automatically, which is provided in this thread.
I am trying to plot a 2y-axes plot; on the left, the actual values, and on the right, the % values. In addition to this, I need to apply coord_trans on the left y-axis for a better visualization of small values. However, when I do it, the labels on the right do not show up.
Here the data (example)
Here the code
DAXIS <- ggplot(x1, aes(hour, value_T, colour=season, linetype = variable)) +
geom_line(size = 1) +
scale_linetype_manual(c("var"), values=c("solid", "dashed", "dotted"))+ # here to change one name
geom_point(aes(shape = season), size = 1)+
labs(x = "hour", y = "T") +
scale_x_continuous(breaks = c(0, 6, 12, 18, 23), labels= c(0, 6, 12, 18, 24))+
scale_y_continuous("T",
sec.axis = sec_axis(~./2.341598, name = " [%] ",
breaks=c(0.2135294, 0.4270588, 0.6405882, 0.8541176,1),
labels = function(b) { paste0(round(b * 100, 0), "%")}))+
#coord_trans(y = "log10", breaks=c(0.5,1,1.5,1.903738), labels = c(0.5,1,1.5,1.9))+ # attemp 1
#coord_trans(y = "log10")+ # attemp 2
scale_color_aaas()+
theme_bw()+
theme(legend.direction = "horizontal", legend.position = "bottom", legend.key = element_blank(),
legend.background = element_rect(fill = "white", colour = "gray30")) +
theme(legend.position="bottom",
text=element_text(size=18),
axis.text.x = element_text(size=15),
axis.text.y = element_text(size=15))
DAXIS
Here the output without coord_trans
Here the output with coord_trans
Any help is very much appreciated
I have small data frame of statistical values obtained from different method. you can download from here.Dataset is look like this:
I need to facet (two plot side by side with same y axis labels) two plot of RMSE.SD and MB variable using ggplot2 package in R like the following example figure.
I wrote this code for plotting 1 plot for RMSE.SD variable.
library(ggplot2)
comparison_korea <- read.csv("comparison_korea.csv")
ggplot(data=comparison_korea, aes(R,X))+
geom_point(color = "black", pch=17, alpha=1,na.rm=T, size=4)+
labs(title = "", y = "")+
theme(plot.title= element_text(hjust = 0.5,size = 15, vjust = 0.5, face= c("bold")),
axis.ticks.length = unit(0.2,"cm") ,
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
axis.text.x = element_text(angle = 0, vjust = 0.5, size = 14, hjust = 0.5,margin=margin(4,0,0,0), colour = "black"),
axis.text.y = element_text(angle = 0, vjust = 0.5, size = 14, hjust = 1,margin=margin(0,5,0,0), colour = "black"),
plot.margin = unit(c(1, 1.5, 1, 0.5), "lines"))
You should be able to do something like this:
library(ggplot2)
ds <- read.csv("comparison_korea.csv")
dat <- data.frame(labels = as.character(ds$X),
RMSE.SD = ds$RMSE.SD,
MB = ds$MB)
dat <- reshape2::melt(dat)
ggplot(dat, aes(y = labels, x = value)) +
geom_point(shape = "+", size = 5) +
facet_wrap(~variable) +
xlab("value / reference (mm)") +
ylab("") +
theme_bw()
I created a ggplot2 object:
a <- replicate(8,rnorm(100))
colnames(a) <- letters[1:8]
b < -melt(a,id.vars=1:1)
colnames(b) <- c("c","variable","value")
ggplot(b,aes(x = c,y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = factor(variable)), size = 1.7) +
scale_x_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand=c(0.01, 0.01)) +
scale_y_continuous(limits = c(-1, 1),
breaks = seq(-1, 1, 0.1),
expand = c(0.01, 0.01))+
theme_bw(base_size = 12, base_family = "Helvetica") +
theme(axis.text=element_text(size = 10),
axis.title=element_text(size = 10),
text = element_text(size = 10),
axis.line = element_line(size = 0.25),
axis.ticks=element_line(size = 0.25),
panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.border = element_rect(colour = "black", fill = NA, size = 0.5),
panel.background = element_blank(),
legend.position = "top" ,
legend.direction = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 13),
legend.background = element_blank(),
legend.key = element_blank()) +
labs(x = '', y = '', title = "") +
theme(plot.title = element_text(size=10)) +
theme(strip.text.x = element_text(size = 8,color="black"),
strip.background = element_blank()) +
theme(strip.text.x = element_text(size = 8, colour = "black"))
My problem is the following:
when I create the legend, there is a separate legend for the colors and a separate one for the points.
How can I create a single legend for each of the 8 variables?
Let me minimise your code and focus on the legend issue. This is what you have now.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line() +
geom_point(aes(shape = factor(variable)),size=1.7)
Your data frame, b has variable as factor. You use this in two ways here; variable and factor(variable). You can simply use variable for shape in geom_point; make all variable identical.
ggplot(b,aes(x = c, y = value, colour = variable, linetype = variable)) +
geom_line()+
geom_point(aes(shape = variable),size = 1.7)
I saw some warning messages related to colours and other things. You may want to take care of them. But, for legend, this is one way to go.
Take from the ideas on this page: http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/#modifying-the-text-of-legend-titles-and-labels
I edited your code to make the data visible (you had problems with your x-axis limits. Note the final three lines. These commands tell ggplot to create only one legend.
a<-replicate(6,rnorm(100))
colnames(a)<-letters[1:6]
b<-melt(a,id.vars=1:1)
colnames(b)<-c("c","variable","value")
ggplot(b,aes(x=c,y=value,colour=variable,linetype=variable)) +
geom_line() + geom_point(aes(shape=factor(variable)),size=1.7)+
scale_x_continuous(limits=c(0,100))+
scale_y_continuous(limits=c(-2,2),breaks=seq(-2,2,0.1),expand=c(0.01,0.01))+
theme_bw(base_size=12, base_family="Helvetica") +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
text = element_text(size=10),
axis.line = element_line(size=0.25),
axis.ticks=element_line(size=0.25),
panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.border = element_rect(colour="black",fill=NA,size=0.5),
panel.background = element_blank(),
legend.position="top" ,
legend.direction="vertical",
legend.title=element_blank(),
legend.text=element_text(size=13),
legend.background=element_blank(),
legend.key=element_blank())+
labs(x='', y='',title="")+
theme(plot.title=element_text(size=10))+
theme(strip.text.x = element_text(size = 8,color="black"),strip.background=element_blank())+
theme(strip.text.x = element_text(size = 8,color="black"))+
scale_colour_discrete(name ="Factor")+
scale_linetype_discrete(name ="Factor") +
scale_shape_discrete(name ="Factor")