Reputation: 519
I have this dataframe:
Control Stress days sd_control sd_stress
X1 -0.2866667 -0.2833333 X1 0.11846237 0.05773503
X2 -0.2566667 -1.0333333 X2 0.08144528 0.15275252
X3 -0.4766667 -1.4500000 X3 0.09291573 0.10000000
X4 -0.4900000 -1.2766667 X4 0.21517435 0.22501852
X5 -0.4600000 -1.2666667 X5 0.07549834 0.40722639
X6 -0.2633333 -1.0833333 X6 0.12662280 0.10408330
X7 -0.2833333 -1.0333333 X7 0.03511885 0.07767453
Based on this data I made this plot:
with this code:
ggplot(data = my_mean, aes(x=days,group=1)) +
geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
width=0.2, size=0.5) +
geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
width=0.2, size=0.5) +
geom_point(aes(y=Control, color = "Control", fill = "Control", shape = "Control"),
size=4) +
geom_line(aes(y=Control, color = "Control"),size=1) +
geom_point(aes(y=Stress, color = "Stress", fill = "Stress", shape = "Stress"),
size=4) +
geom_line(aes(y=Stress, color = "Stress"), size=1) +
geom_point(data=significance, aes(y=value),shape='*',size=6) +
scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_shape_manual(values = c("Control" = 23, "Stress" = 22)) +
guides(shape = FALSE, fill = FALSE,
color = guide_legend(override.aes = list(shape = c("Control" = 23,
"Stress" = 22),
fill = c("Control" = 'gray45',
"Stress" = 'gray')))) +
labs(x='\nDAT',y='ΨMpa\n') +
scale_y_continuous(limits = c(-2,-0), expand = c(0,0),
breaks = seq(from=0,to=-2.5,by=-0.2)) +
scale_x_discrete(expand = c(0.07, 0), labels = c(0,7,14,21,27,35,42),
position = "top") +
ggtitle('Leaf Water Potential\n') +
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_rect(fill = 'white'),
plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
axis.title = element_text(family = 'Calibri',face = 'bold',
axis.title.x.bottom =TRUE),
axis.text = element_text(family = 'Calibri'),
legend.text = element_text(family = 'Calibri',face = 'bold'),
legend.title = element_blank(),
legend.position = c(0.9, 0.13),
legend.key = element_rect(fill = NA,color = NA)
)
Note how I included the position="top"
argument in scale_x_discrete
because I want the axis text to appear on top, but I want the axis title ("DAT") back on the bottom. I guess the axis.title.x.bottom
theme function could do the trick but I was unable to find how to pass it to the code and what arguments does it need. Any suggestions with that?
Upvotes: 3
Views: 2214
Reputation: 4524
I think the best way is to use a secondary axis without an axis title scale_x_continuous(sec.axis = dup_axis(name =''), breaks=c(0,7,14, 21, 28, 35, 42), labels=c('0','7','14', '21', '28', '35', '42'))
and to switch the text of the x-axis of: axis.text.x.bottom = element_blank()
.
I used scale_x_continuous
and changed days by df$days <- seq(0, 42, 7)
.
If you want, you can switch the ticks on the bottom x-axis off, too.
library(ggplot2)
df <- read.table(text="Control Stress days sd_control sd_stress
-0.2866667 -0.2833333 X1 0.11846237 0.05773503
-0.2566667 -1.0333333 X2 0.08144528 0.15275252
-0.4766667 -1.4500000 X3 0.09291573 0.10000000
-0.4900000 -1.2766667 X4 0.21517435 0.22501852
-0.4600000 -1.2666667 X5 0.07549834 0.40722639
-0.2633333 -1.0833333 X6 0.12662280 0.10408330
-0.2833333 -1.0333333 X7 0.03511885 0.07767453", header=T)
df$days <- seq(0,42,7)
ggplot(data = df, aes(x=days,group=1)) +
geom_errorbar(aes(ymax = Control-sd_control, ymin = Control+sd_control),
width=0.2, size=0.5) +
geom_errorbar(aes(ymax = Stress-sd_stress, ymin = Stress+sd_stress),
width=0.2, size=0.5) +
geom_point(aes(y=Control, color = "Control", fill = "Control", shape = "Control"),
size=4) +
geom_line(aes(y=Control, color = "Control"),size=1) +
geom_point(aes(y=Stress, color = "Stress", fill = "Stress", shape = "Stress"),
size=4) +
geom_line(aes(y=Stress, color = "Stress"), size=1) +
#geom_point(data=significance, aes(y=value),shape='*',size=6) +
scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray') ) +
scale_shape_manual(values = c("Control" = 23, "Stress" = 22)) +
guides(shape = FALSE, fill = FALSE,
color = guide_legend(override.aes = list(shape = c("Control" = 23,
"Stress" = 22),
fill = c("Control" = 'gray45',
"Stress" = 'gray')))) +
labs(x='\nDAT',y='ΨMpa\n') +
scale_y_continuous(limits = c(-2,-0), expand = c(0,0),
breaks = seq(from=0,to=-2.5,by=-0.2)) +
#scale_x_discrete(expand = c(0.07, 0), labels = c(0,7,14,21,27,35,42)) +
scale_x_continuous(sec.axis = dup_axis(name =''),
breaks=c(0,7,14, 21, 28, 35, 42),
labels=c('0','7','14', '21', '28', '35', '42')) +
ggtitle('Leaf Water Potential\n') +
theme(panel.border = element_rect(colour = "black", fill=NA, size=0.5),
panel.background = element_rect(fill = 'white'),
plot.title = element_text(hjust = 0.5,family = 'Calibri',face='bold'),
#axis.title.x.bottom = element_blank(),
axis.text.x.bottom = element_blank(),
#axis.title = element_text(axis.title.x.bottom =TRUE),
#axis.text = element_text(family = 'Calibri'),
legend.text = element_text(family = 'Calibri',face = 'bold'),
legend.title = element_blank(),
legend.position = c(0.9, 0.13),
legend.key = element_rect(fill = NA,color = NA)
)
PS: I switched the third data line singificance off; don't know where it comes from
Upvotes: 2
Reputation: 1007
What you could do is simulate an x-axis title at the bottom using a caption, then just remove the one at the top. These additions do the trick:
ggplot() +
... +
labs(caption = "DAT") +
theme(axis.title.x = element_blank(),
plot.caption = element_text(size = 14, hjust = 0.5, face = "bold"),
...)
This is what it should look like:
Upvotes: 1