Prevent repeated facets in ggplot2 when using facet_wrap - r

I have repeated facet headers that I need to combine into a single header with the following sample data:
structure(list(Level1 = structure(c(1L, 1L, 1L, 1L, 1L), .Label =
"Repeated", class = "factor"), Level2 = structure(c(1L, 1L, 2L, 2L, 2L),
.Label = c("A", "B"), class = "factor"), Level3 = structure(1:5, .Label
= c("A", "B", "C", "D", "E"), class = "factor"), Value = c(6L, 2L, 3L,
4L, 0L)), .Names = c("Level1", "Level2", "Level3", "Value"), class =
"data.frame", row.names = c(NA, -5L))
I created a barplot with ggplot2 using facet_wrap() and set the number of columns to 1:
ggplot(data=samp, aes(x=reorder(Level3, Value), y=Value)) +
geom_bar(stat="identity") +
coord_flip() +
ylab("Value") +
xlab("Level3") +
facet_wrap(~Level1 + Level2, scales = 'free_y', ncol = 1)
I need to show the nesting for the Level3, Level2, and Level1 factors but without repeating any facet headers. Here the Level1 factor facet header is repeated.
I attempted to follow instructions from the post below, but could not figure out how to customize to my data: Nested facets in ggplot2 spanning groups
Any assistance is appreciated.

Related

Draw scatter plot and set color based one categorial column in ggplot2

Given a dataframe as follows:
structure(list(city = structure(c(1L, 3L, 4L, 2L), .Label = c("bj",
"cq", "sh", "tj"), class = "factor"), area = c(1580.86, 1927.95,
532.24, 613.09), price = c(9.51, 94.42, 10.77, 8.58), level = structure(c(1L,
1L, 2L, 2L), .Label = c("a", "b"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))
I want to draw a scatter plot which x for area and y for price, at same time, the color of points only based on level, which means only two colors to distinguish a and b.
How could I draw this with ggplot2? Thanks.
You can use the following code for that
library(ggplot2)
ggplot(df, aes(x = area, y = price, col=level)) + geom_point()
ggplot(df, aes(x = area, y = price, col=level)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = city))
Data
df=structure(list(city = structure(c(1L, 3L, 4L, 2L), .Label = c("bj",
"cq", "sh", "tj"), class = "factor"), area = c(1580.86, 1927.95,
532.24, 613.09), price = c(9.51, 94.42, 10.77, 8.58), level = structure(c(1L,
1L, 2L, 2L), .Label = c("a", "b"), class = "factor")), class = "data.frame", row.names = c(NA,
-4L))

plotting 3 variables on a single plot in ggplot2

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))

asterix R bar ggplot [duplicate]

I tried to make the title self-explanatory, but here goes - data first:
dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"),
ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("srednja škola", "fakultet"), class = "factor"),
`(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139,
33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692,
23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable",
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df",
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma",
"fla"), class = "factor"), ustanova = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola",
"fakultet"), class = "factor")), .Names = c("variable", "ustanova"
), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola",
"vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola",
"fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"),
structure(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))
And I'd like to create a dodged barplot, do the coord_flip and put some text labels inside the bars:
ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +
coord_flip()
you can see output here.
I reckon I'm asking for something trivial. I want the text labels to "follow" stacked bars. Labels are placed correctly on the y-axis, but how to position them correctly on x-axis?
Is this what you want?
library(ggplot2)
ggplot(bar) +
geom_col(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`), group = ustanova),
position = position_dodge(width = .9)) +
coord_flip()
The key is to position = position_dodge(width = .9) (where .9 is the default width of the bars) instead of position = "dodge", which is just a shortcut without any parameter. Additionally you have to set the group=ustanova aesthetic in geom_text to dodge the labels by ustanova (A second option would be to make fill = ustanova a global aesthetic via ggplot(bar, aes(fill = ustanova))
In ggplot2_2.0.0 you find several examples in ?geom_text on how to position geom_text on dodged or stacked bars (the code chunk named "# Aligning labels and bars"). The Q&A What is the width argument in position_dodge? provides a more thorough description of the topic.

R: ggplot annotate geom_text with position_dodge and facets

I have an R dataframe data (made with dplyr) that I'm trying to plot with ggplot():
require(dplyr)
data <- structure(list(gGroup = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L), .Label = c("MC", "R", "UC"), class = "factor"),
Episode = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("Morning", "Day", "Night", "24 hour"
), class = "factor"), variable = c("HF", "HF", "LF", "LF",
"HF", "HF", "LF", "LF", "HF", "HF", "LF", "LF"), parameter = c("RR",
"RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR",
"RT"), mean = c(3.90575222833804, 4.24572828952087, 5.14491629837998,
3.88189313775535, 4.02908403079823, 3.91129824615597, 4.73913642980089,
3.63973850905423, 4.66445796048274, 4.21723744674943, 5.57765585365275,
4.01444148455851), sd = c(1.09129154084895, 1.43102672123806,
1.17782114274004, 1.33381488706382, 1.33497319178289, 1.22259231099975,
1.33329948427898, 1.09625319168102, 1.19876558625356, 1.73746797295816,
1.05862249404741, 1.91144835753868), se = c(0.199241664579179,
0.261268538538247, 0.215039736195078, 0.243520167060353,
0.471984298305965, 0.432251656867227, 0.471392553343098,
0.387584032867524, 0.215304655178374, 0.312058460044998,
0.190134212775724, 0.343306259564318)), .Names = c("gGroup",
"Episode", "variable", "parameter", "mean", "sd", "se"), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), drop = TRUE, indices = list(
0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L,
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
gGroup = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("MC",
"R", "UC"), class = "factor"), Episode = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Morning", "Day", "Night",
"24 hour"), class = "factor"), variable = c("HF", "LF", "HF",
"LF", "HF", "LF")), .Names = c("gGroup", "Episode", "variable"
), class = "data.frame", row.names = c(NA, -6L)))
Currently I'm using the following code to plot:
require(ggplot2)
require(ggthemes)
pd <- position_dodge(width=0.9)
p <- ggplot(data, aes(x = gGroup, y = mean, fill = variable)) +
facet_grid(parameter~Episode) +
geom_bar(stat="identity", position=pd) +
geom_errorbar(aes(ymin = mean-se, ymax = mean+se), width = .3, position=pd) +
theme_hc() + scale_fill_hc() +
labs(y = "Logit transform of spectral power (m/s2), mean±SE", x= NULL)
ann_text <- data.frame(gGroup = "MC", mean = 6, variable = "LF", parameter = "RR", Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2), data = ann_text, label="*", position=pd)
This gives me the following plot:
I'm quite satisfied with the result, but as you can see the asterisk isn't aligned correctly. I looked it up online, and I read this and this and the manual.
Everyone I see the suggestions to use position=position_dodge(width=0.9), but this doesn't help for me. I tried hjust to maybe move the asterisk to the right position, but that's of no use either. Funny thing is that my error bars are aligned correctly.
I feel like I'm overlooking something very simple, but I cannot figure out what it is.
I'm using R 3.1.3 on OSX 10.10.2, and loading the newest versions of ggplot2 and ggthemes.
In order for position_dodge to work, there needs to be a reason to dodge. That is you need to change the ann_text appropriately with variable = c("LF", "HF"), so that there is a reason to dodge. Then just define the label appropriately. Below I assume you just want the * over the LF bar.
ann_text <- data.frame(gGroup = rep("MC",2),
mean = 6,
variable = c("LF", 'HF'),
label = c("*", ""),
parameter = "RR",
Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2, label = label), data = ann_text, position=pd)

Position geom_text on dodged barplot

I tried to make the title self-explanatory, but here goes - data first:
dtf <- structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma", "fla"), class = "factor"),
ustanova = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), .Label = c("srednja škola", "fakultet"), class = "factor"),
`(all)` = c(42.9542857142857, 38.7803203661327, 37.8996138996139,
33.7672811059908, 29.591439688716, 26.1890660592255, 27.9557692307692,
23.9426605504587, 33.2200772200772, 26.9493087557604)), .Names = c("variable",
"ustanova", "(all)"), row.names = c(NA, 10L), class = c("cast_df",
"data.frame"), idvars = c("variable", "ustanova"), rdimnames = list(
structure(list(variable = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L, 5L, 5L), .Label = c("vma", "vla", "ia", "fma",
"fla"), class = "factor"), ustanova = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("srednja škola",
"fakultet"), class = "factor")), .Names = c("variable", "ustanova"
), row.names = c("vma_srednja škola", "vma_fakultet", "vla_srednja škola",
"vla_fakultet", "ia_srednja škola", "ia_fakultet", "fma_srednja škola",
"fma_fakultet", "fla_srednja škola", "fla_fakultet"), class = "data.frame"),
structure(list(value = structure(1L, .Label = "(all)", class = "factor")), .Names = "value", row.names = "(all)", class = "data.frame")))
And I'd like to create a dodged barplot, do the coord_flip and put some text labels inside the bars:
ggplot(bar) + geom_bar(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`)), position = "dodge") +
coord_flip()
you can see output here.
I reckon I'm asking for something trivial. I want the text labels to "follow" stacked bars. Labels are placed correctly on the y-axis, but how to position them correctly on x-axis?
Is this what you want?
library(ggplot2)
ggplot(bar) +
geom_col(aes(variable, `(all)`, fill = ustanova), position = "dodge") +
geom_text(aes(variable, `(all)`, label = sprintf("%2.1f", `(all)`), group = ustanova),
position = position_dodge(width = .9)) +
coord_flip()
The key is to position = position_dodge(width = .9) (where .9 is the default width of the bars) instead of position = "dodge", which is just a shortcut without any parameter. Additionally you have to set the group=ustanova aesthetic in geom_text to dodge the labels by ustanova (A second option would be to make fill = ustanova a global aesthetic via ggplot(bar, aes(fill = ustanova))
In ggplot2_2.0.0 you find several examples in ?geom_text on how to position geom_text on dodged or stacked bars (the code chunk named "# Aligning labels and bars"). The Q&A What is the width argument in position_dodge? provides a more thorough description of the topic.

Resources