Editing graph using ggpattern in R - r

I wrote some code to make a graph (both below)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(fun = "mean", geom = "bar", position = "dodge") +
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
However, I recently read about 'ggpattern' and wondered if anyone could help me add some diagonal black lines to the yellow bars in my plot (e.g. ex-smokers conflict). I have tried multiple ways, but adding 'geom_col_pattern' to the code seems to mess up the Y axis and provide overall conflict for each condition (animal, smoking) rather than separately for smokers and ex-smokers. I think the 'geom_col_pattern' perhaps is not compatible with the 'stat_summary' I have in my code. Does anyone have any suggestions?
Thank you

Instead of adding a geom_col_pattern on top of your plot, just update the geom argument of stat_summary.
#replicate of your dataframe
for_plots <- data.frame(matrix(nrow = 100, ncol=0))
for_plots$condition <- sample(rep(c("Animal", "Smoking"), 100), 100)
for_plots$smoking_status <- sample(rep(c("Smokers", "Ex"), 100), 100)
n_smoking <- length(which(for_plots$condition == "Smoking"))
for_plots$conflict[for_plots$condition=="Smoking"] <- sample(seq(0.8, 1.3, length.out = n_smoking), n_smoking)
n_animal <- length(which(for_plots$condition == "Animal"))
for_plots$conflict[for_plots$condition=="Animal"] <- sample(seq(0.5, 1, length.out = n_animal), n_animal)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(aes(pattern=smoking_status),
fun = "mean", position = "dodge",
geom = "bar_pattern", pattern_fill="black", colour="black") + #edited part
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_pattern_manual(values=c("none", "stripe"))+ #edited part
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))

Related

log transform X axis R

I have the following raw data that I plotted in R:
And I would like to edit this plot to look like this version below which was made by log-transforming the X axis using Excel
However, when I run my code below using scale_x_log10(), the output is not the desired plot I was hoping to make. See image below:
Can anyone identify where I have gone wrong?
ggplot(data = data, aes(x = x, y = y, group = group, color = group)) +
stat_summary(fun = "mean", geom = "line", size = 1.2, aes(group = group, linetype = group, color = group)) +
stat_summary(fun = "mean", geom = "point", size = 3, aes(color = group)) +
theme_apa() +
scale_linetype_manual(values = c("solid", "dashed")) +
scale_color_manual(values = c("mediumturquoise", "red")) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
scale_x_log10(limits = c(.01, 40), breaks = c(.01, .1, 1, 10))
It looks like your first datapoint is at zero - this can't be displayed on a log scale. You'll need to work out if there's a difference in you data in excel, failing that you could achieve a similar result by modifying the lowest value of x with:
ggplot(data = data, aes(x = pmax(x,0.01), y = y, group = group, color = group)) +
stat_summary(fun = "mean", geom = "line", size = 1.2, aes(group = group, linetype = group, color = group)) +
stat_summary(fun = "mean", geom = "point", size = 3, aes(color = group)) +
theme_apa() +
scale_linetype_manual(values = c("solid", "dashed")) +
scale_color_manual(values = c("mediumturquoise", "red")) +
scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) +
scale_x_log10(limits = c(.01, 40), breaks = c(.01, .1, 1, 10))

Manually changing line size in ggplot

Still getting to grips with ggplot. My question: How do I manually change the line size? I've tried with scale_size_manual but it didn't seem to work.
setup:
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
specify size inside aes() function as follows:
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type, size = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
Just turning #NelsonGon comment into an answer.
Is this what you want?
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
# -------------------------------------------------------------------------
base <- ggplot(test.mat, aes(x=Window, y=value))
#Here is where you need to add size
line_size <- base + geom_line(aes(colour = type, linetype = type), size=3)
line_size + theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
output
Update
If you want variable thickness for the individual lines, you can do as follows.
base <- ggplot(test.mat, aes(x=Window, y=value))
#Use an ifelse to add variable thickness
line_size <- base + geom_line(aes(colour = type, size=ifelse(type=="Cohort Alpha",1,2)))
line_size + guides(size = FALSE)
To follow up on my comment on deepseefan's answer
base +
geom_line(aes(colour = type,
size=factor(ifelse(type=="Cohort Alpha", "thick", "thin"),
levels=c("thick","thin")))) +
scale_colour_manual(values = c("black", "steelblue")) +
scale_size_manual(values = c(5, 1.4), guide = FALSE)

How to scale a Geom_bar to be in line with an overlaid line graph in R ggplot

I am trying to overlay a bar chart with a line graph on a single plot with ggplot in R. My line graph works fine but the data are much larger than the data for the bar chart component.
How could I use an additional scale for this bar chart or do something that will get this to look nice all in one graph.
Here is my plot code thus far:
chart <- data.frame("QuantileName" = 1:5, "AvgLoss" = c(100, 500, 1000, 2500, 3000), "AvgFactor" = c(1.0, 1.1, 1.3, 1.4, 1.5))
Plot <- ggplot(chart, aes(x = 1:5)) +
scale_x_continuous(name = "Quintile", limits = c(0, 5 + .5), breaks = seq(1, 5)) +
geom_line(aes(y = AvgLoss, colour = "AvgLoss")) +
geom_bar(aes(y = AvgFactor, colour = "AvgFactor" ), stat = "identity") +
geom_text(aes(y = AvgLoss, label = round(AvgLoss)), position = position_nudge(x = .3)) +
geom_point(aes(y = AvgLoss)) +
ylab("AvgLoss") +
scale_colour_manual("",breaks = c("AvgLoss","AvgFactor"), values = c("AvgLoss" = "red", "AvgFactor" = "grey")) +
ggtitle("Quintile Plot") +
theme(plot.title = element_text(hjust=0.5))
Plot
Thank you for any help!
Essentialy, multiply your AvgFactor variable by a number
+ geom_bar(aes(y = AvgFactor*1000, colour = "AvgFactor" ), stat = "identity")
and set
+ scale_y_continuous(sec.axis = sec_axis(~ ./1000, name = "AvgFactor"))
so your plot code would look like
Plot <- ggplot(chart, aes(x = 1:5)) +
scale_x_continuous(name = "Quintile", limits = c(0, 5 + .5),
breaks = seq(1, 5)) +
geom_bar(aes(y = AvgFactor*1000, colour = "AvgFactor" ),
stat = "identity") +
geom_line(aes(y = AvgLoss, colour = "AvgLoss")) +
geom_text(aes(y = AvgLoss,
label = round(AvgLoss)),
position = position_nudge(x = .3)) +
geom_point(aes(y = AvgLoss)) +
ylab("AvgLoss") +
scale_colour_manual("",breaks = c("AvgLoss","AvgFactor"),
values = c("AvgLoss" = "red", "AvgFactor" = "grey")) +
ggtitle("Quintile Plot") +
theme(plot.title = element_text(hjust=0.5)) +
scale_y_continuous(sec.axis = sec_axis(~ ./1000, name = "AvgFactor"))
However, I think it is probably more elegant to avoid secondary axes whenever possible.
It may be useful to know that geom_col(...) is shorthand for geom_bar(..., stat = 'identity')

ggplot legend / label change with various guides

I am trying to change the labels of a legend on a ggplot where I have legends for 2 aes.
With scale_file_manual, it works, but for one of the two legends only. I guess I should use "guides" that I already used to remove the titles and also remove legend for a 3rd aes, but I do not manage to do it.
Do you have a solution?
Here is my code :
p <- ggplot(data, aes(x = Nb))
p + geom_ribbon(aes(ymin = Sandwich.min, ymax = Sandwich.max, fill = 'grey70',alpha=0.8)) +
geom_ribbon(aes(ymin = Assiette.min, ymax = Assiette.max, fill = '#6495ED80',alpha=0.8)) +
geom_line(aes(y = Pizza, col = '#FF7F24')) +
geom_line(aes(y = Sushis, col = '#228B22')) +
labs(title = "Business lunch cost by number of participants",
x = "Number of participants",
y = "Price (Euros)") +
scale_x_continuous(breaks=seq(1,20,1)) +
scale_y_continuous(breaks = seq(0,300,50)) +
theme_light() +
theme(plot.title = element_text(size = 12, hjust = 0.5)) +
guides(alpha = FALSE, colour = guide_legend(" "), fill = guide_legend(" ")) +
scale_fill_manual(
values=c('#6495ED80','grey70'),
labels=c("Assiettes","Sandwiches"))

ggplot - Remove alpha legend

I have created a plot with the following dataset:
Locus;Island;AR;Type;Shapetype
MS1;ST;4,6315;MS;NA
MS1;FG;3,9689;MS;NA
MS1;SN;3;MS;NA
MS2;ST;2;MS;NA
MS2;FG;2;MS;NA
MS2;SN;2;MS;NA
MS3;ST;7,5199;MS;NA
MS3;FG;5,5868;MS;NA
MS3;SN;3;MS;NA
MS4;ST;2,9947;MS;NA
MS4;FG;3;MS;NA
MS4;SN;2;MS;NA
MS5;ST;9,0726;MS;NA
MS5;FG;5,6759;MS;NA
MS5;SN;2,963;MS;NA
MS6;ST;6,5779;MS;NA
MS6;FG;5,6842;MS;NA
MS6;SN;2;MS;NA
MS7;ST;2;MS;NA
MS7;FG;1;MS;NA
MS7;SN;1;MS;NA
MS8;ST;3,97;MS;NA
MS8;FG;2,9032;MS;NA
MS8;SN;1;MS;NA
MS9;ST;2;MS;NA
MS9;FG;1,9977;MS;NA
MS9;SN;2;MS;NA
MS10;ST;3,9733;MS;NA
MS10;FG;3,9971;MS;NA
MS10;SN;2;MS;NA
MS11;ST;7,4172;MS;NA
MS11;FG;5,6471;MS;NA
MS11;SN;3;MS;NA
MS12;ST;2;MS;NA
MS12;FG;2;MS;NA
MS12;SN;2;MS;NA
MS13;ST;5,6135;MS;NA
MS13;FG;3;MS;NA
MS13;SN;2;MS;NA
MT;ST;12;MT;NA
MT;FG;3;MT;NA
MT;SN;2;MT;NA
TLR1LA;ST;3,68;TLR;TLR1LA
TLR1LA;FG;4,4;TLR;TLR1LA
TLR1LA;SN;1;TLR;TLR1LA
TLR1LB;ST;3,99;TLR;TLR1LB
TLR1LB;FG;5;TLR;TLR1LB
TLR1LB;SN;1;TLR;TLR1LB
TLR2A;ST;4,9;TLR;TLR2A
TLR2A;FG;5;TLR;TLR2A
TLR2A;SN;2;TLR;TLR2A
TLR2B;ST;5,64;TLR;TLR2B
TLR2B;FG;4;TLR;TLR2B
TLR2B;SN;3;TLR;TLR2B
TLR3;ST;1;TLR;TLR3
TLR3;FG;3;TLR;TLR3
TLR3;SN;3;TLR;TLR3
TLR4;ST;1;TLR;TLR4
TLR4;FG;2,89;TLR;TLR4
TLR4;SN;2;TLR;TLR4
TLR5;ST;2,9;TLR;TLR5
TLR5;FG;2;TLR;TLR5
TLR5;SN;2;TLR;TLR5
TLR21;ST;2,91;TLR;TLR21
TLR21;FG;1;TLR;TLR21
TLR21;SN;1;TLR;TLR21
Here's the code for the plot:
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1)) +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
The plot is okay, however, I need to remove the legend that is created for the alpha values. I have tried to use both + scale_alpha(guide = 'none')and guide = 'none', but none of them seem to work (I may be placing them in the wrong places, though). I suspect that they do not work, because of the manual adjustment of the alpha values.
Please be aware that this is not a minimal example.
Please note that your alpha legend is also your size legend, but this is very hard to see since your sizes are very similar. Set guide = 'none' in both scale_alpha_manual and scale_size_manual to remove that portion of the legend.
If you only do it in scale_alpha_manual you can actually see that the alpha becomes 1 for those lines, so it works as intended. So #Thierry's answer is correct.
Full code
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = 'none') +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3), guide = 'none') +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
Result
(Note that my y-axis is wrong because your data includes comma's and I was lazy.)
guide = "none" should do the trick
ggplot(
comb,
aes(Island, AR, group = Locus, colour = (factor(Type)))
) +
geom_line(aes(alpha = factor(Type), size = factor(Type))) +
geom_point(aes(shape = factor(Shapetype))) +
scale_x_discrete(
"Island",
labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")
) +
ylab("Allelic Richness") +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = "none") +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
scale_shape_manual(
"Functional",
values = c(1,2,3,4,5,6,7,8,9,10),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
scale_colour_manual(
"Neutral",
values = c("Red","Blue","Black"),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
theme_bw()
Starting with this basic plot
bp <- df %>%
ggplot(aes(column_of_interest, alpha = 0.25)) + geom_density()
from r cookbook, where bp is your ggplot -
Remove legend for a particular aesthetic (alpha):
bp + guides(alpha="none")

Resources