Dataframe
df <- data.frame(
structure(list(biological_group = structure(1:15, .Label = c("A",
"B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O"), class = "factor"), norm_expression = c(2L, 3L, 4L, 6L,
1L, 5L, 7L, 8L, 9L, 3L, 2L, 6L, 7L, 8L, 1L), 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), Group = structure(c(1L,
1L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("",
"Plant Products", "Sugars"), class = "factor")), class = "data.frame", row.names = c(NA,
-15L), .Names = c("biological_group", "norm_expression", "SE",
"Group")))
Sample code
library(ggplot2)
DFplot <- ggplot(df, aes(biological_group,norm_expression)) +
ylab("Relative Normalized\nExpression (∆∆Cq)") +
geom_bar(fill="black",stat="identity") +
theme(axis.title.x = element_blank())
DFplot2 <- DFplot+geom_errorbar(aes(ymin=norm_expression-SE,ymax=norm_expression+SE),width=0.5) +
geom_boxplot() +
facet_grid(~Group, scales = "free_x", switch = "x", space = "free_x") +
scale_y_continuous(expand = c(0,0), labels = scales::number_format(accuracy = 0.1)) +
theme_classic()
That gives me this graph:
I'd like to remove the vertical lines from the strip text labels (specified by strip.background), like this:
I realize I could just use photoshop or something, but I have several graphs to make so it would be easier if I could just specify it in the code.
This answer was helpful.
In your case, you want to find the strip-b for your bottom strips to substitute.
Edit: Replaced top bar of bottom strip that was removed accidentally.
library(grid)
q <- ggplotGrob(DFplot2)
lg <- linesGrob(x=unit(c(1,0),"npc"), y=unit(c(1,1),"npc"), gp=gpar(col="black", lwd=2))
for (k in grep("strip-b",q$layout$name)) {
q$grobs[[k]]$grobs[[1]]$children[[1]] <- lg
}
grid.draw(q)
Related
I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.
Dataframe:
structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE",
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L,
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L,
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"),
id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L,
4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape",
"Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
), class = "factor")), .Names = c("item", "location", "category",
"id"), class = "data.frame", row.names = c(NA, -12L))
my_data %>%
ggplot(aes(item, location, shape = category, label = id)) +
geom_col(aes(y = Inf), fill = "gray80") +
geom_point(size = 3) +
geom_text(vjust = -1) +
scale_x_discrete(expand = expand_scale(add = c(5,0))) +
coord_polar(theta = "y") +
theme_void()
If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:
my_data %>%
mutate(item_pos = as.numeric(item),
item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
ggplot(aes(item_pos, location, shape = category, label = id)) +
...
Maybe you can work from this:
ggplot(data,aes(x=location, color=id, y=id)) +
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) +
geom_point(size=3) +
coord_polar()
I have a data like this
df<-structure(list(X = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 10L,
9L, 11L, 12L, 8L), .Label = c("A", "B", "C", "D", "E", "F", "GG",
"IR", "MM", "TT", "TTA", "UD"), class = "factor"), X_t = c(3.7066,
3.6373, 3.2693, 2.5626, 2.4144, 2.2868, 2.1238, 1.8671, 1.7627,
1.4636, 1.4195, 1.0159), NEW = structure(c(8L, 7L, 9L, 1L, 2L,
3L, 4L, 5L, 6L, 10L, 11L, 12L), .Label = c("12-Jan", "14-Jan",
"16-Jan", "19-Jan", "25-Jan", "28-Jan", "4-Jan", "Feb-38", "Feb-48",
"Jan-39", "Jan-41", "Jan-66"), class = "factor")), class = "data.frame", row.names = c(NA,
-12L))
I am trying to put the label for each dot but I get a warning
here is how I plot it
ggplot(data=df)+
geom_point(aes(X_t, X,size =X_t,colour =X_t,label = NEW))
also I want to merge the two legend into one because it is redundant, if you have any tips let me know please
Use geom_text for text (e.g., labels):
ggplot(data=df, aes(X_t, X)) +
geom_point(aes(size = X_t, colour = X_t)) +
geom_text(aes(label = NEW), nudge_y = 0.5) +
guides(color = guide_legend(), size = guide_legend())
Aesthetics you specify in the ggplot() call will be inherited by subsequent layeres (geoms). So by putting the x and y aesthetics in ggplot(), we don't have to re-specify them again.
As for the legend question, see this answer for details. To combine color and size legends we use guide_legend.
Hi have an experiment which consists of three variables, and I would like to plot them all on a single plot.
This is my df:
AB <- data.frame(block=c("A", "A", "A", "A", "B", "B", "B", "B" ),
familiarity=c("fam", "fam", "unfam", "unfam" ),
prime=c("P", "UP" ),
RT=c("570.6929", "628.7446", "644.6268", "607.4312", "556.3581", "645.4821", "623.5624", "604.4113"))
Right now I can only break one of the variables into two separate plots, like this where A and B are the two levels of the third variable:
A <- AB[which(AB$block == "A"),]
B <- AB[which(AB$block == "B"),]
pa <- ggplot(data=A, aes(x=prime, y=RT, group=familiarity)) +
geom_line(aes(linetype=familiarity), size=1) +
expand_limits(y=c(500,650))
pb <- ggplot(data=B, aes(x=prime, y=RT, group=familiarity)) +
geom_line(aes(linetype=familiarity), size=1) +
expand_limits(y=c(500,650))
I would like to superimpose plot A over plot B, and have this third variables to be identified by color.
Any ideas?
Is this what you mean?
p_all <- ggplot(AB, aes(x=prime,y=RT,group=interaction(familiarity,block))) +
geom_line(aes(linetype=familiarity,color=block))
Data used:
AB <- structure(list(block = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("A", "B"), class = "factor"), familiarity = structure(c(1L,
1L, 2L, 2L, 1L, 1L, 2L, 2L), class = "factor", .Label = c("fam",
"unfam")), prime = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L
), class = "factor", .Label = c("P", "UP")), RT = c(570.6929,
628.7446, 644.6268, 607.4312, 556.3581, 645.4821, 623.5624, 604.4113
)), .Names = c("block", "familiarity", "prime", "RT"), row.names = c(NA,
-8L), class = "data.frame")
IF you have different datasets for those variables, then you can specify the data
ggplot()+
geom_line(data=A, aes(x=prime, y=RT, group=familiarity,linetype=familiarity), size=1) +
geom_line(data=B, aes(x=prime, y=RT, group=familiarity,linetype=familiarity), size=1)+
expand_limits(y=c(500,650))
Thanks to combine stacked bars and dodged bars, I created the plot below using the data frame shown. But now, since the axis titles name the bars, how can I remove the legend elements other than for the one stacked bar? That is, can the legend show only the segments of the Big8 bar?
> dput(combo)
structure(list(firm = structure(c(12L, 1L, 11L, 13L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("Avg.", "Co", "Firm1",
"Firm2", "Firm3", "Firm4", "Firm5", "Firm6", "Firm7", "Firm8",
"Median", "Q1", "Q3"), class = "factor"), metric = structure(c(5L,
1L, 4L, 6L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Avg.",
"Big8", "Co", "Median", "Q1", "Q3"), class = "factor"), value = c(0.0012,
0.0065, 0.002, 0.0036, 0.0065, 0.000847004466666667, 0.000658907411111111,
0.0002466389, 8.41422555555556e-05, 8.19149222222222e-05, 7.97185555555556e-05,
7.82742555555556e-05, 7.56679888888889e-05), grp = structure(c(1L,
2L, 3L, 6L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Q1",
"Avg.", "Median", "Co", "Big8", "Q3"), class = "factor")), .Names = c("firm",
"metric", "value", "grp"), row.names = c(NA, -13L), class = "data.frame")
Here is the plotting code.
ggplot(combo, aes(x=grp, y=value, fill=firm)) +
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2))
The plot, which ideally would have a smaller set of elements in the legend.
You can manually set the breaks for scale_fill_discrete:
library(ggplot2)
ggplot(combo, aes(x=grp, y=value, fill=firm)) +
geom_bar(stat="identity") +
labs(x = "", y = "") +
theme(legend.position = "bottom") +
guides(fill = guide_legend(nrow = 2)) +
scale_fill_discrete(breaks = combo$firm[combo$metric=="Big8"])
I'm not 100% sure which labels you want to keep, but a manually entered vector, combo$firm and combo$metric will all work.
dput(x)
structure(list(Date = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L), .Label = c("1/1/2012", "2/1/2012", "3/1/2012"
), class = "factor"), Server = structure(c(1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L), .Label = c("A", "B", "C", "D"), class = "factor"),
Storage = c(10000L, 20000L, 30000L, 15000L, 15000L, 25000L,
35000L, 15700L, 16000L, 27000L, 37000L, 16700L)), .Names = c("Date",
"Server", "Storage"), class = "data.frame", row.names = c(NA,
-12L))
I would like to create a stack bar x=Date, y=Storage and alos place a linear line based on the total storage.
I have come up with this ggplot line:
ggplot(x, aes(x=Date, y=Storage)) + geom_bar(aes(x=Date,y=Storage,fill=Server), stat="identity", position="stack") + geom_smooth(aes(group=1),method="lm", size=2, color="red")
It kinda works but linear line is not based on total storage for a given Date on the date frame x. Is there an easy way to do this?
Often the easiest way is just to calculate the values outside of ggplot2. So calculate the totals:
dd = as.data.frame(tapply(x$Storage, x$Date, sum))
dd$Date = rownames(dd)
colnames(dd)[1] = "Storage"
then add a geom_smooth call but specify the data:
ggplot(x, aes(x=Date, y=Storage)) +
geom_bar(aes(x=Date,y=Storage, fill=Server), stat="identity", position="stack") +
geom_smooth(data = dd, aes(x=Date, y=Storage, group=1),method="lm")