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?
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
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:
Related
I want to group these bars by the variable "env".
ggplot(dat, aes(x = Sample, fill = phylum, y = Abundance)) +
geom_bar(position="fill", stat = "identity") + theme_bw() +
facet_wrap( ~ env) +
theme(axis.text.x = element_text(angle = 90, size = 13, colour = "black", vjust = 0.5, hjust = 1), axis.title.x = element_text(size = 15),
axis.text.y = element_text(size = 13, vjust = 0.5, hjust = 1), axis.title.y = element_text(size = 15), legend.title = element_text(size = 15),
legend.text = element_text(size = 15, colour = "black")) +
ggtitle("Minion samples: Proteobacteria Phyla") + scale_y_continuous(labels = percent_format(), limits=c(0,1)) +
scale_fill_manual(values = c("Acidobacteria"="#3288bd",
"Actinobacteria" = "#99d594",
"Candidatus Rokubacteria" = "#74c476",
"Chloroflexi"= "#e6f598",
"Planctomycetes"="#fee08b",
"Proteobacteria" = "#fc8d59",
"Verrucomicrobia" = "#a50f15",
"Taxa less than 1%" = "#d53e4f"))
I get this:
I would like to have an unique x-axis and get rid of these empty spaces. Is there an other option beside facet_wrap?
Thanks
As per the comments, indeed using facet_wrap(~env, scales = "free, nrow =1) will solve your issue.
Moreover, you might want to have an equal width for the histograms, in this case you can use facet_grid and the space argument to have something like this:
ggplot(dat, aes(x = Sample, fill = phylum, y = Abundance)) +
geom_bar(position="fill", stat = "identity") + theme_bw() +
facet_grid(. ~ env, scales = "free", space = "free")+
theme(axis.text.x = element_text(angle = 90, size = 13, colour = "black", vjust = 0.5, hjust = 1), axis.title.x = element_text(size = 15),
axis.text.y = element_text(size = 13, vjust = 0.5, hjust = 1), axis.title.y = element_text(size = 15), legend.title = element_text(size = 15),
legend.text = element_text(size = 15, colour = "black")) +
ggtitle("Minion samples: Proteobacteria Phyla") + scale_y_continuous(labels = percent_format(), limits=c(0,1)) +
scale_fill_manual(values = c("Acidobacteria"="#3288bd",
"Actinobacteria" = "#99d594",
"Candidatus Rokubacteria" = "#74c476",
"Chloroflexi"= "#e6f598",
"Planctomycetes"="#fee08b",
"Proteobacteria" = "#fc8d59",
"Verrucomicrobia" = "#a50f15",
"Taxa less than 1%" = "#d53e4f"))
Whereas, facet_wrap(~ env, scales = "free", nrow = 1) results in unequal width distribution when free on one row:
ggplot(dat, aes(x = Sample, fill = phylum, y = Abundance)) +
geom_bar(position="fill", stat = "identity") + theme_bw() +
facet_wrap(~ env, scales = "free", nrow = 1)+
theme(axis.text.x = element_text(angle = 90, size = 13, colour = "black", vjust = 0.5, hjust = 1), axis.title.x = element_text(size = 15),
axis.text.y = element_text(size = 13, vjust = 0.5, hjust = 1), axis.title.y = element_text(size = 15), legend.title = element_text(size = 15),
legend.text = element_text(size = 15, colour = "black")) +
ggtitle("Minion samples: Proteobacteria Phyla") + scale_y_continuous(labels = percent_format(), limits=c(0,1)) +
scale_fill_manual(values = c("Acidobacteria"="#3288bd",
"Actinobacteria" = "#99d594",
"Candidatus Rokubacteria" = "#74c476",
"Chloroflexi"= "#e6f598",
"Planctomycetes"="#fee08b",
"Proteobacteria" = "#fc8d59",
"Verrucomicrobia" = "#a50f15",
"Taxa less than 1%" = "#d53e4f"))
Data
library(scales)
library(ggplot2)
set.seed(18)
dat = data.frame(
env = factor(rep(sample(c("Dry", "Fossil", "Wet", "Soil"), size = 25, prob = c(3,2,18,2), replace = T), each=8), levels =c("Dry", "Fossil", "Wet", "Soil")),
Sample = rep(paste0("Sample_",letters[1:25]), each=8),
phylum = rep(c("Acidobacteria", "Actinobacteria","Candidatus Rokubacteria","Chloroflexi","Planctomycetes", "Proteobacteria", "Verrucomicrobia","Taxa less than 1%"),times = 25),
Abundance = runif(200,0,1))
I tried various options, but I cannot find a way to achieve custom legend appearance (unless I export the figure to power point and edit it there...). I would like the legend to look like in the image below and wonder if that is at all possible:
I do not wish to make any changes in the figure itself:
Here is my sample data and code:
df = data.frame(sex = c(1,1,1,1,1, 2,2,2,2,2),
age_cat = c(1,1,1, 2,2,2, 1,1,1, 2),
score_type = c(1,2, 1,2, 1,2, 1,2, 1,2),
score = c(25,28,18,20,30, 37,40,35,43,45))
df$sex <- factor((df$sex))
df$age_cat <- factor((df$age_cat))
df$score_type <- factor((df$score_type))
windows(width=7, height=7)
library(ggplot2)
df %>%
ggplot( aes(x=score_type, y=score)) +
geom_boxplot(aes(color=sex),outlier.shape = NA, size=1.5, show.legend=T) +
geom_point(aes(color=sex, shape = age_cat, group = sex),
position=position_jitterdodge(dodge.width=0.9), size=3, show.legend=F) +
scale_color_manual(values=c("#0072B2", "#CC79A7"), name="",
labels=c("Male", "Female")) +
scale_shape_manual(name="", labels=c('Younger', 'Older'),
values=c(16, 17)) +
theme_bw()+
theme(panel.border = element_blank(), axis.ticks = element_blank(),
legend.position=c(0.9, 0.65), legend.text=element_text(size=11),
legend.title=element_text(size=11.5),
panel.grid.major.x = element_blank() ,
plot.title = element_text(size=11, face = "bold"),
axis.title=element_text(size=13),
axis.text.y = element_text(size=11),
axis.text.x = element_text(size=11),
plot.margin = unit(c(0.5,0.2,0,0.2), "cm")) +
labs(title= "", x = "",y = "Score") +
scale_y_continuous(breaks=c(0, 20, 40, 60, 80, 100),
labels=c('0', '20', '40', '60', '80', '100')) +
expand_limits(x=5, y=70) +
scale_x_discrete(labels = c("A", "B")) +
coord_cartesian(clip = "off")
You could achieve your desired result by
dropping show.legend=FALSE from geom_point
Overriding the shapes to be displayed in the legend using guides(shape = guide_legend(override.aes = list(shape = c(1, 2))))
library(ggplot2)
ggplot(df, aes(x = score_type, y = score)) +
geom_boxplot(aes(color = sex), outlier.shape = NA, size = 1.5) +
geom_point(aes(color = sex, shape = age_cat, group = sex),
position = position_jitterdodge(dodge.width = 0.9), size = 3
) +
scale_color_manual(
values = c("#0072B2", "#CC79A7"), name = "",
labels = c("Male", "Female")
) +
scale_shape_manual(
name = "", labels = c("Younger", "Older"),
values = c(16, 17)
) +
theme_bw() +
theme(
panel.border = element_blank(), axis.ticks = element_blank(),
legend.position = c(0.9, 0.65), legend.text = element_text(size = 11),
legend.title = element_text(size = 11.5),
panel.grid.major.x = element_blank(),
plot.title = element_text(size = 11, face = "bold"),
axis.title = element_text(size = 13),
axis.text.y = element_text(size = 11),
axis.text.x = element_text(size = 11),
plot.margin = unit(c(0.5, 0.2, 0, 0.2), "cm")
) +
labs(title = "", x = "", y = "Score") +
scale_y_continuous(
breaks = c(0, 20, 40, 60, 80, 100),
labels = c("0", "20", "40", "60", "80", "100")
) +
expand_limits(x = 5, y = 70) +
scale_x_discrete(labels = c("A", "B")) +
coord_cartesian(clip = "off") +
guides(shape = guide_legend(override.aes = list(shape = c(1, 2))))
I have this plot:
that was made based on this data:
days variable value sd
1 X1 Control 75.03424 3.857730
2 X2 Control 70.17851 2.913420
3 X3 Control 65.01627 9.188947
4 X4 Control 65.70995 10.882072
5 X5 Control 56.98791 8.070014
6 X6 Control 56.64376 4.827183
7 X1 Stress 75.63113 3.207749
8 X2 Stress 70.56030 5.626266
9 X3 Stress 61.56402 7.078610
10 X4 Stress 48.04541 15.287234
11 X5 Stress 43.54458 8.148382
12 X6 Stress 37.51121 9.494008
With this code:
significance <- data.frame(days=c("X4","X5","X6"),value=c(82,70,67), variable=NA)
# Plot
library(ggplot2)
library(extrafont)
library(scales)
library(Cairo)
ggplot(my_mean, aes(x=days, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge', width = 0.75) +
geom_errorbar(aes(ymin = value-sd, ymax = value+sd),
position = position_dodge(0.75),
width = 0.3) +
labs(x='\nDAT',y='μg/cm2\n') +
scale_y_continuous(limits = c(0,90), expand = c(0,0),
breaks = seq(from=0,to=90,by=10)) +
scale_x_discrete(labels = c(0,7,14,21,27,35)) +
ggtitle('Chlorophyll Content\n') +
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')) +
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.text = element_text(family = 'Calibri'),
legend.text = element_text(family = 'Calibri',face = 'bold'),
legend.position = c(0.92, 0.91),
legend.key = element_rect(fill = NA,color = NA),
legend.title = element_blank(),
legend.background = element_blank()
)
Note how I made a dataframe to map my significance points in the plot. But for some reason, these points also were added to the legend, and don't want this to happens. I suspect this could be fixed with some legend function inside the theme configurations, but I wasn't able to find which function to use and what arguments does it would have.
Any suggestion? Or maybe a better approach to map my significance "stars"?
If you don't want the stars to appear in the legend, try geom_point(..., show.legend = F). In fact, show.legend = F is an option in many ggplot2 layers.
You can try this:
ggplot(my_mean, aes(x=days, y=value, fill=variable)) +
geom_bar(stat='identity', position='dodge', width = 0.75) +
geom_errorbar(aes(ymin = value-sd, ymax = value+sd),
position = position_dodge(0.75),
width = 0.3,color='black') +
labs(x='\nDAT',y='μg/cm2\n') +
scale_y_continuous(limits = c(0,90), expand = c(0,0),
breaks = seq(from=0,to=90,by=10)) +
scale_x_discrete(labels = c(0,7,14,21,27,35)) +
ggtitle('Chlorophyll Content\n') +
geom_point(data=significance, aes(y=value),shape='*',size=6,color='black') +
scale_color_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
scale_fill_manual(values = c("Control" = 'gray45', "Stress" = 'gray')) +
guides(fill=guide_legend(override.aes=list(shape=NA)))+
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.text = element_text(family = 'Calibri'),
legend.text = element_text(family = 'Calibri',face = 'bold'),
legend.position = c(0.92, 0.91),
legend.key = element_rect(fill = NA,color = NA),
legend.title = element_blank(),
legend.background = element_blank()
)
I have this dataframe:
Control Stress days sd_control sd_stress
X1 0.9702100 0.9343627 X1 0.001900535 0.07035645
X2 0.9666619 0.8595523 X2 0.014946893 0.04066567
X3 0.9165654 0.7160598 X3 0.072655343 0.07025344
X4 0.9208237 0.6668044 X4 0.050870831 0.08736982
X5 0.8766547 0.7660685 X5 0.073588197 0.04868614
X6 0.9599553 0.7937444 X6 0.041559836 0.05326769
X7 0.9736297 0.8188934 X7 0.003817743 0.06272428
and based on this data I've done this plot:
With the following code:
significance <- data.frame(days=c("X2","X3","X4","X6"),value=c(1.02,1.02,1.02,1.02))
ggplot(my_data, aes(x=days,y=Control,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(shape=23,color='gray45',fill='gray45',size=4) +
geom_line(color='gray45',size=1) +
geom_point(data=my_data,aes(x=days,y=Stress),size=4,shape=22,fill='gray',color='gray',
show.legend = TRUE) +
geom_line(data = my_data, aes(x=days,y=Stress),color='gray',size=1) +
geom_point(data=significance, aes(x=days,y=value),shape='*',size=6) +
labs(x='\nDAT',y='RWC\n') +
scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04),
expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
scale_x_discrete(expand = c(0.07, 0),labels = c(0,7,14,21,27,35,42)) +
ggtitle('Relative Water Content\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.text = element_text(family = 'Calibri')
)
I want to add a legend in the bottom-right on the plot that describres the Control and Stress Treatmentes with the same shape of the points. I've tried several approaches that I've found here as set a color vector and scale_colour_manual attributes but none of them worked. Any suggestion?
The issue is that you use the color, fill and shape arguments.
To get a legend you have to map on aesthetics, i.e. inside aes().
After doing so ggplot will add lgends(s) automatically and you can apply scale_xxx_manual to get the desired colors, fill and shapes.
However, as this results in 3 legends (was not able to figure out why the merging of the legends failed) I use guides to keep only one of them and guide_legend to style the legend. Try this:
library(ggplot2)
library(scales)
ggplot(my_data, 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='RWC\n') +
scale_y_continuous(labels = percent_format(accuracy = 1),limits = c(0.5,1.04),
expand = c(0,0), breaks = seq(from=0.5,to=1,by=0.05)) +
scale_x_discrete(expand = c(0.07, 0), labels = c(0,7,14,21,27,35,42)) +
ggtitle('Relative Water Content\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.text = element_text(family = 'Calibri')
)
I can create quality control charts with the qicharts2 package.
library(tidyverse)
library(qicharts2)
(plot1 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
ylab = 'Years',
xlab = 'Patient #'
)
)
p1 <- plot1$data
Then I can customize the charts.
(plot2 <- ggplot(p1, aes(x, y)) +
geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1) +
geom_line(aes(x, cl)) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p1 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0))
)
Using the part argument, in my qichart, causes it to split at the specified part point(s).
(plot3 <- qic(age,
data = tail(cabg, 100),
chart = 'i',
part = c(70, 85),
ylab = 'Years',
xlab = 'Patient #'
)
)
p3 <- plot3$data
What do I need to add to my customized ggplot2 syntax, below, to get it to part in the same manner? What I've got does everything, EXCEPT, it doesn't part like in the syntax directly above.
(plot4 <- ggplot(p3, aes(x, y)) +
geom_ribbon(ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1) +
geom_line(aes(x, cl)) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0))
)
Is the following plot what you are looking for?
If so, what I used is group= in the aesthetics of geom_ribbon and geom_line
(plot4 <- ggplot(p3, aes(x, y)) +
geom_ribbon(aes(group=cut(p3$x,c(0,70,85,max(p3$x)))),ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) +
geom_line(color = "black", size = 1, aes(group=cut(p3$x,c(0,70,85,max(p3$x))))) +
geom_line(aes(x, cl, group=cut(p3$x,c(0,70,85,max(p3$x))))) +
geom_point(color = "black" , fill = "black", size = 2) +
geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) +
ggtitle(label = NULL) +
labs(x = NULL, y = NULL) +
scale_y_continuous(breaks = seq(0, 100, by = 10)) +
coord_cartesian(ylim = c(0, 100)) +
theme_bw() +
theme(
text = element_text(size = 18),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6),
axis.text.y = NULL,
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.text.x = element_text(size = 14, color = "black", angle = 0)))