Related
I am trying to make scales for the y-axis free. But it looks like it is not working. I want adaptation to have its own scale and post adaptation also having its own scales. I am trying to make scales for the y-axis free. But it looks like it is not working. I want adaptation to have its own scale and post adaptation also having its own scales.
tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("Visible", "Remembered"), class = "factor"),
Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667,
2.84197270833333, 0.307057416666667, 2.403112375, 0.281202,
3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155,
1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333,
2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333,
1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643,
0.0894916121701392, 1.14554677132408, 0.0958562360654162,
1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828,
1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737,
0.833908172662699, 0.0798994165789182, 1.11582277105041,
0.0976064300150272, 0.667812406644538, 0.142929179817685,
0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993,
0.0258340031883217, 0.330690868396632, 0.0276713118479362,
0.308385789857611, 0.0275145288174349, 0.341226302469221,
0.0258846474942731, 0.383546697661249, 0.0317155495718416,
0.174748037086728, 0.0235401482506832, 0.240728553983119,
0.0230649748349663, 0.322110288616933, 0.0281765493219072,
0.192780836372198, 0.0412601002213964, 0.198043748767058,
0.0316950341456936, 0.402728684306467, 0.0467234944577166
), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983,
0.0568602576432562, 0.727845693918804, 0.0609041467375754,
0.678752547059741, 0.0605590696140879, 0.751034027967696,
0.0569717250090983, 0.844180589754564, 0.069805453951774,
0.384617836383033, 0.0518115169661108, 0.529839974927164,
0.0507656673296478, 0.708959965158704, 0.0620161669201078,
0.424307760005262, 0.0908128682911871, 0.435891352085212,
0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA,
-24L), class = "data.frame")
library(ggh4x)
p <- ggplot(tgc, aes(x = Condition, y = EE), fill = test) +
geom_errorbar(aes(ymin=EE-se, ymax=EE+se, group = test), position = position_dodge(0.5), width=.1) +
geom_bar(aes(fill = test), stat = "identity", width = 0.5, color = "black", position='dodge') + ylim(0,4) + theme_bw() + theme(
axis.text.x = element_text(size = 12,face="bold"),#, angle = 10, hjust = .5, vjust = .5),
axis.text.y = element_text(size = 12, face = "bold"),
axis.title.y = element_text(vjust= 1.8, size = 16),
axis.title.x = element_text(vjust= -0.5, size = 16),
axis.title = element_text(face = "bold")) + xlab("Workspace") + ylab("EE (cm)") + theme(legend.position="top") +
scale_fill_manual(values = c("grey80", "grey20")) + facet_nested(. ~ Session + Group, scales = "free_y") + theme(aspect.ratio = 6/4)
p + guides(fill=guide_legend(title="Test:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
theme(strip.text = element_text(face="bold", size=12))
The facet_nested() function is based on facet_grid(), in which you can have y scales that vary between rows in the grid, but not within a row.
Let's make the base of the plot.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.1.1
library(ggh4x)
tgc <- structure(list(Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("Visible", "Remembered"), class = "factor"),
Condition = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
), .Label = c("CEN", "IPS", "CTL"), class = "factor"), test = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Pre-test", "Post-test"
), class = "factor"), Session = structure(c(1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("Adaptation", "Post-adaptation"
), class = "factor"), N = c(12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12), EE = c(2.134379625, 0.333942625, 1.742841125, 0.317361916666667,
2.84197270833333, 0.307057416666667, 2.403112375, 0.281202,
3.49590529166667, 0.305657666666667, 2.85211466666667, 0.3131155,
1.44857545833333, 0.269328166666667, 1.740270875, 0.243361833333333,
2.10702266666667, 0.286209125, 2.145855125, 0.305474083333333,
1.60016616666667, 0.281528625, 1.94182179166667, 0.294655916666667
), sd = c(0.727246182828044, 0.0816168443914292, 0.549168068103643,
0.0894916121701392, 1.14554677132408, 0.0958562360654162,
1.06827971273128, 0.0953131237162305, 1.18204258551111, 0.0896670491921828,
1.32864473484909, 0.109865886496798, 0.605344957514288, 0.0815454655757737,
0.833908172662699, 0.0798994165789182, 1.11582277105041,
0.0976064300150272, 0.667812406644538, 0.142929179817685,
0.686043669971901, 0.109794818975944, 1.39509308576833, 0.161854932615856
), se = c(0.209937889711449, 0.0235607535398997, 0.158531165974993,
0.0258340031883217, 0.330690868396632, 0.0276713118479362,
0.308385789857611, 0.0275145288174349, 0.341226302469221,
0.0258846474942731, 0.383546697661249, 0.0317155495718416,
0.174748037086728, 0.0235401482506832, 0.240728553983119,
0.0230649748349663, 0.322110288616933, 0.0281765493219072,
0.192780836372198, 0.0412601002213964, 0.198043748767058,
0.0316950341456936, 0.402728684306467, 0.0467234944577166
), ci = c(0.462070179795855, 0.0518568689018959, 0.348924743722983,
0.0568602576432562, 0.727845693918804, 0.0609041467375754,
0.678752547059741, 0.0605590696140879, 0.751034027967696,
0.0569717250090983, 0.844180589754564, 0.069805453951774,
0.384617836383033, 0.0518115169661108, 0.529839974927164,
0.0507656673296478, 0.708959965158704, 0.0620161669201078,
0.424307760005262, 0.0908128682911871, 0.435891352085212,
0.0697602998032695, 0.886399857701764, 0.102837717929058)), row.names = c(NA,
-24L), class = "data.frame")
p <- ggplot(tgc, aes(x = Condition, y = EE), fill = test) +
geom_errorbar(aes(ymin=EE-se, ymax=EE+se, group = test),
position = position_dodge(0.5), width=.1) +
geom_bar(aes(fill = test),
stat = "identity", width = 0.5, color = "black", position='dodge') +
theme_bw() + theme(
axis.text.x = element_text(size = 12,face="bold"),
axis.text.y = element_text(size = 12, face = "bold"),
axis.title.y = element_text(vjust= 1.8, size = 16),
axis.title.x = element_text(vjust= -0.5, size = 16),
axis.title = element_text(face = "bold")) +
xlab("Workspace") + ylab("EE (cm)") +
theme(legend.position="top") +
scale_fill_manual(values = c("grey80", "grey20")) +
theme(aspect.ratio = 6/4) +
guides(fill=guide_legend(title="Test:")) +
theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
theme(strip.text = element_text(face="bold", size=12))
If you wanted to have a non-grid layout, it is probably easier to use facet_nested_wrap() instead.
p + facet_nested_wrap(~Session + Group, scales = "free_y", nrow = 1)
Alternatively, if you must retain the grid layout and want the y-axes to be independent within a row, you can use independent = "y".
p + facet_nested(~Session + Group, scales = "free_y", independent = "y")
Created on 2021-11-09 by the reprex package (v2.0.1)
It is not directly an answer to that question, but an alternative would be to create the panels separately. You can do this semi-programmatically by creating a list first. The biggest disadvantage that I can see in this way is that patchwork still cannot merge / combine x-/y- axis titles like it does with legends, so you would need to create each plot separately which would in turn defeat the point of this approach...
library(tidyverse)
library(ggh4x)
library(patchwork)
ls_p <-
tgc %>%
split(., .$Session) %>%
map(function(x){
## I've tried to de-clutter your code.
ggplot(x, aes(x = Condition, y = EE, fill = test)) +
geom_errorbar(aes(ymin = EE - se, ymax = EE + se, group = test), position = position_dodge(0.5), width = .1) +
geom_col(width = 0.5, color = "black", position = "dodge") +
labs(x= "Workspace", y = "EE (cm)") +
scale_fill_manual("Test:", values = c("grey80", "grey20")) +
facet_nested(. ~ Session + Group, scales = "free_y") +
## I am using cowplot::theme_minimal because I think it's a well designed theme
cowplot::theme_minimal_hgrid()
})
wrap_plots(ls_p) + plot_layout(guides = "collect") &
theme(legend.position = "top")
UPDATED:
Data has now been updated to full chemistry values as opposed to mean values.
I am attempting to create a box and whisker plot in r, on a very small dataset. My data is not behaving itself or I am missing some glaringly obvious error.
This is the code i have for making said plot
library(ggplot2)
Methanogenesis_Data=read.csv("CO2-CH4 Rates.csv")
attach(Methanogenesis_Data)
summary(Methanogenesis_Data)
str(Methanogenesis_Data)
boxplot(CH4rate~Patch+Temperature, data = Methanogenesis_Data,
xlab="Patch", ylab="CH4 Production")
cols<-c("red", "blue")
From this small dataset.
structure(list(Patch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gravel", "Macrophytes",
"Marginal"), class = "factor"), Temperature = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cold",
"Warm"), class = "factor"), CH4rate = c(0.001262595, 0.00138508,
0.001675944, 0.001592354, 0.002169233, 0.001772964, 0.002156633,
0.002864403, 0.002301383, 0.002561042, 0.005189598, 0.004557227,
0.008484851, 0.006867866, 0.007438633, 0.005405327, 0.006381582,
0.008860084, 0.007615417, 0.007705906, 0.009198508, 0.00705233,
0.007943024, 0.008319768, 0.010362114, 0.007822153, 0.010339339,
0.009252302, 0.008249555, 0.008197657), CO2rate = c(0.002274825,
0.002484866, 0.003020209, 0.00289133, 0.003927232, 0.003219346,
0.003922613, 0.005217026, 0.00418674, 0.00466427, 0.009427322,
0.008236453, 0.015339532, 0.012494729, 0.013531303, 0.009839847,
0.011624428, 0.016136746, 0.0138831, 0.014051034, 0.016753211,
0.012780956, 0.01445912, 0.01515584, 0.01883252, 0.014249452,
0.018849478, 0.016863299, 0.015045964, 0.014941168)), .Names = c("Patch",
"Temperature", "CH4rate", "CO2rate"), class = "data.frame", row.names =
c(NA,
-30L))
The plot I get as output is good, however I would like the Variables on the X axis to simply display "Gravel" "Macrophytes" "Marginal" as opposed to each of those variables with Warm and Cold. Thanks for any assistance
THIS IS WHAT I AM TRYING TO ACHEIVE -----> Exact Boxplot I want to create
Following your update with an example graph :
I have also included the formating for the legend position. If you want to edit the y axis label to include subscript I would suggest you read over this. I have included a blank title for relabelling.
test <- structure(list(Patch = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Gravel", "Macrophytes",
"Marginal"), class = "factor"), Temperature = structure(c(2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("Cold",
"Warm"), class = "factor"), CH4rate = c(0.001262595, 0.00138508,
0.001675944, 0.001592354, 0.002169233, 0.001772964, 0.002156633,
0.002864403, 0.002301383, 0.002561042, 0.005189598, 0.004557227,
0.008484851, 0.006867866, 0.007438633, 0.005405327, 0.006381582,
0.008860084, 0.007615417, 0.007705906, 0.009198508, 0.00705233,
0.007943024, 0.008319768, 0.010362114, 0.007822153, 0.010339339,
0.009252302, 0.008249555, 0.008197657), CO2rate = c(0.002274825,
0.002484866, 0.003020209, 0.00289133, 0.003927232, 0.003219346,
0.003922613, 0.005217026, 0.00418674, 0.00466427, 0.009427322,
0.008236453, 0.015339532, 0.012494729, 0.013531303, 0.009839847,
0.011624428, 0.016136746, 0.0138831, 0.014051034, 0.016753211,
0.012780956, 0.01445912, 0.01515584, 0.01883252, 0.014249452,
0.018849478, 0.016863299, 0.015045964, 0.014941168)), .Names = c("Patch",
"Temperature", "CH4rate", "CO2rate"), class = "data.frame", row.names =
c(NA,
-30L))
Now I will create two data sets one for each graph just for simplicity you could leave them combined and facet but for formatting purposes this might be easier.
CH4rate <- test %>%
gather("id", "value", 3:4) %>%
filter(id == "CH4rate")
CO2rate <- test %>%
gather("id", "value", 3:4) %>%
filter(id == "CO2rate")
First plot:
ggplot(CH4rate) +
geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
labs(title = "Title of graph", x="Patch Type", y = "CH4rate") +
scale_fill_manual(name = "", values = c("orange", "light blue")
, labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))
Second plot:
ggplot(CO2rate) +
geom_boxplot(mapping = aes(x = Patch, y = value, fill=factor(Temperature, levels = c("Warm", "Cold")))) +
theme(legend.position = c(0.15, 0.9), panel.background = element_rect(fill = "white", colour = "grey50")) +
labs(title = "Title of graph", x="Patch Type", y = "CO2rate") +
scale_fill_manual(name = "", values = c("orange", "light blue")
, labels = c("Cold" = "Incubated at 10˙C", "Warm" = "Incubated at 26˙C"))
I am having trouble getting my plots to work, I have multiple categorical variables by which I want to color by one, and facet by another. However, R keeps adding the "values" (I used melt) for the same variables together instead. It works when I only have one variable.
Here is my plot with one variable
Here is my plot with two variables, you can see the adding that is happening
simple dataframe
Here is my code:
library(reshape2)
library(ggplot2)
test2 <- structure(list(SampleID = c(12.19, 12.22, 13.1, 12.19, 12.22,
13.1, 12.19, 12.22, 13.1, 12.19, 12.22, 13.1), patient = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), type = structure(c(1L,
1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L), .Label = c("L",
"T"), class = "factor"), timepoint = structure(c(1L, 2L, 2L,
1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L), .Label = c("1", "2"), class = "factor"),
Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "D", class = "factor"), variable = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("A",
"B", "C", "D", "E", "F", "G", "H", "I"), class = "factor"),
value = c(2L, 5L, 6L, 25L, 18L, 12L, 6L, 10L, 15L, 21L, 23L,
33L)), .Names = c("SampleID", "patient", "type", "timepoint",
"Group", "variable", "value"), row.names = c(NA, 12L), class = "data.frame")
ggplot(test2, aes(test2$variable, test2$value, fill=test2$timepoint)) +
geom_bar(stat="identity", position = "dodge") +
scale_fill_manual(values=c("rosybrown1", "steelblue2", "gray")) +
labs(x="Category", y="Count", title = paste0("Sample ", as.character(unique(test2$patient)) , " - " , as.character(unique(test2$Group)))) +
facet_wrap(~test2$type) +
theme(text = element_text(size=15),
axis.text.x = element_text(angle = 90, hjust = 1, vjust=.5, size = 7))
If I am understanding right, it looks like you just need to give the scales option to facet_wrap like so:
facet_wrap(~type, scales = "free_x")
I have boxplot and I would like to add pvalues for 4 comparisons across two factors.
Here is the data set:
dput(CauloQ_datMannot)
structure(list(V1 = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L,
3L, 4L, 4L, 4L), .Label = c("B", "BF", "BFi ", "Bi"),
class = "factor"),
variable = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L), .Label = c("V2", "V3", "V4"), class = "factor"),
value = c(0.00051, 0.00055, 0.00056, 0.00074, 0.00079, 0.00083,
0.00093, 0.00082, 0.00073, 0.0011, 0.00113, 0.00098),
Location = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Root", class = "factor"),
Bean = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = "Bean", class = "factor"), Fungi = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("M+",
"M-"), class = "factor"), Insect = structure(c(2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect",
"NI"), class = "factor")), .Names = c("V1", "variable", "value",
"Location", "Bean", "Fungi", "Insect"), row.names = c(NA, -12L
), class = "data.frame")
Here is my current graph:
ggplot(CauloQ_datMannot,aes(x=Insect,y=value,fill=Fungi))+geom_boxplot()+
guides(fill=guide_legend("Metarhizium")) +
ggtitle("Caulobacter qPCR")+
scale_x_discrete(labels= c("I+","I-","soil alone"))+
theme(plot.title = element_text(size = 18, face = "bold"))+
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14)) +
theme(legend.text=element_text(size=14),
legend.title=element_text(size=14)) +
theme(strip.text.x = element_text(size = 14))
I have installed ggpubr, and have read up on compare_stat_means, but can't figure out how to make comparisons involving the two factors. That is I want 4 pvalues
M+/I+ vs M-/I+, and M+/I- vs M-/I-, and I+/M+ vs I-/M+, and I+/M- vs I-/M-
Any help is appreciated. thanks
>
Great. Now thanks to Jimbou, I have the following plot.
d %>% unite(groups, Insect, Fungi, remove = F) %>%
{ggplot(.,aes(groups, value, fill= Fungi)) +
geom_boxplot() + # ggbeeswarm::geom_beeswarm()+
ggsignif::geom_signif(comparisons = combn(sort(unique(.$groups)),2, simplify = F),
step_increase = 0.1,test='t.test')}
However, I would like to re-order the boxes, ie. with all I+ ones first (M+ first within that). I tried re-ordering the levels and then manually the rows, that neither worked.
Any help appreciated
d$Insect<-factor(d$Insect,levels(d$Insect)[c(2,1)])
d$Fungi<-factor(d$Fungi,levels(d$Fungi)[c(2,1)])
I recommend to use well defined groups on the x-axis. Then you can try
library(tidyverse)
library(ggsignif)
library(ggbeeswarm)
d %>%
unite(groups, Insect, Fungi, remove = F) %>%
{ggplot(.,aes(groups, value, fill= Fungi)) +
geom_boxplot() +
ggbeeswarm::geom_beeswarm()+
ggsignif::geom_signif(comparisons = combn(sort(unique(.$groups)), 2, simplify = F),
step_increase = 0.1)}
I have a dataset of >100 different samples. Samples are from different genotypes (e.g. X, Y, Z) and 4 different time points (T0,1,2,3) with 3 biological replicates (R1,2,3). I'm measuring values for 50 different genes (in rows; A,B..)
longdata <- structure(list(Gene = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), .Label = c("A", "B"), class = "factor"), Genotype = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("X", "Y", "Z"), class = "factor"),
Time = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("T0",
"T1", "T2", "T3"), class = "factor"), Ave = c(1.32606106633333,
1.499956424, 1.118528738, 1.025082136, 0.424537206666667,
0.723243112666667, 0.335509156333333, 0.328275209, 0.788329993666667,
1.125292329, 2.357924224, 0.678921448, 0.222768019, 0.293117217,
0.548228048, 0.841192647333333, 3.144197864, 0.576764958333333,
1.32037215366667, 1.15039119233333, 1.03539976366667, 1.00032109266667,
0.740699933666667, 0.687992671666667), SE = c(0.119785209010494,
0.168580466330281, 0.264739468221289, 0.124588107424543,
0.194995686650518, 0.0392007703821249, 0.06203362889702,
0.0482287534807508, 0.396968455138007, 0.0903480171168777,
0.717823561374135, 0.164024037188693, 0.0078580995264886,
0.0980939303386436, 0.233081861930954, 0.0870744069976396,
0.324195222544884, 0.434640930315622, 0.0658409437053185,
0.135850334794207, 0.175517934316736, 0.123213160632528,
0.133598346586129, 0.203707785326976)), .Names = c("Gene",
"Genotype", "Time", "Ave", "SE"), class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -24L))
How can I modify this syntax to generate each graph separately and save them as JPG/PNG files?
longdata %>% ggplot(aes(x = Time, y = Ave, fill = Genotype)) + geom_bar(position = position_dodge(), stat = "identity") + geom_errorbar(aes(ymin = Ave - SE, ymax = Ave + SE), width = 0.1, position = position_dodge(0.9)) + facet_wrap(~ Gene)
You can put ggplot and ggsave within a loop.
lapply(sort(unique(longdata$Gene)), function(i){
ggplot(longdata[longdata$Gene == i, ], aes(x = Time, y = Ave, fill = Genotype)) + geom_bar(position = position_dodge(), stat = "identity") + geom_errorbar(aes(ymin = Ave - SE, ymax = Ave + SE), width = 0.1, position = position_dodge(0.9))
ggsave(filename = paste0(i, ".png"))
})
This loop gets the unique elements of Gene, sorts them, create a plot, then save the result.