Reputation: 97
This is my data (in dput format):
structure(list(Biological.Group = structure(1:15, .Label = c("A",
"C", "E", "D", "F", "G", "W", "R", "T", "Y", "V", "B", "N", "M",
"L"), class = "factor"), Group = c(NA, NA, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Relative.Normalized.Expression....Cq. = c(5L,
7L, 3L, 5L, 6L, 8L, 9L, 1L, 4L, 5L, 7L, 4L, 2L, 7L, 8L), SE = c(0.171499719,
0.089692493, 0.153777208, 0.188012958, 0.153776128, 0.192917199,
0.224766056, 0.231338325, 0.121716906, 0.094763028, 0.09635363,
0.069986333, 0.113681329, 0.094614957, 0.391182473)), row.names = c(NA,
-15L), class = "data.frame")
This is the code for my graph:
library(ggplot2)
colours <- c("A"="darkolivegreen4","C"="darkolivegreen2","E"="darkseagreen4","D"="darkseagreen3", "F"="gold3","G"="gold", "W"="goldenrod2","R"="yellow2","T"="mediumpurple","Y"="thistle1", "V"="turquoise","B"="pink3", "N"="plum4", "M"="tomato3", "L"="orange1")
df$Biological.Group <- as.character(df$Biological.Group)
df$Biological.Group <- factor(df$Biological.Group,
levels=unique(df$Biological.Group))
plot = ggplot(df, aes(Biological.Group,Relative.Normalized.Expression....Cq.,fill=factor(Biological.Group)))+ scale_fill_manual(values = colours) + xlab("Condition") +ylab("Relative Normalized Expression (∆∆Cq)")+geom_bar(stat="identity")
plot + geom_errorbar(aes(ymin=Relative.Normalized.Expression....Cq.-SE,ymax=Relative.Normalized.Expression....Cq.+SE),width=0.5)+geom_boxplot() + theme(axis.text.x = element_text(angle = 60, hjust = 1)) + theme(legend.position="none") +ggtitle("Mrdf-1")+theme(plot.title = element_text(size=12, face="italic",hjust=0.5))
ggsave("dfGraph.png",height=5.64,width=5.64)
This is what the graph looks like:
I want to include the "Group" column data (2nd column of data) as secondary labels in the x axis like this:
If possible, I'd also like to make the space between groups bigger (meaning between "A" and "C", "C" and "E", and "G" and "W")
Upvotes: 1
Views: 318
Reputation: 33822
I've made some alterations to your ggplot()
.
factor()
geom_boxplot()
geom_col()
is less typing than geom_bar(stat = "identity")
theme()
lineslabs()
for x, y and title labelsThe labeling of Group can be achieved using facet_grid()
.
Not sure what you mean by "space between groups" - from your question it sounds more like "space between bars" - which can be achieved by adjusting width.
My code:
library(dplyr)
df %>%
mutate(Group = ifelse(is.na(Group), "", Group)) %>%
ggplot(aes(Biological.Group,
Relative.Normalized.Expression....Cq.,
fill = Biological.Group)) +
geom_col(width = 0.7) +
geom_errorbar(aes(ymin = Relative.Normalized.Expression....Cq.-SE,
ymax = Relative.Normalized.Expression....Cq.+SE), width = 0.5) +
scale_fill_manual(values = colours) +
labs(x = "Condition",
y = "Relative Normalized Expression (∆∆Cq)",
title = "Mrdf-1") +
theme(axis.text.x = element_text(angle = 60,
hjust = 1),
legend.position = "none",
plot.title = element_text(size = 12,
face = "italic",
hjust = 0.5),
strip.background.x = element_blank()) +
facet_grid(~Group,
scales = "free_x",
switch = "x",
space = "free_x")
Result:
Upvotes: 3