I'm trying to make a graph similar to this one, however when I try to add a line connecting the before and after species, only one vertical line per species appears. I added the "pair" column to identify each point in the before and after for each species.
Can anyone identify where the error is in my script? Thanks!
structure(list(species = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("1diphylla", "2calycioides", "3duckeana"
), class = "factor"), ID = c("p20", "p15", "p23", "p24", "p25",
"p26", "p3", "p5", "p7", "p8", "p9", "p10", "p11", "p14", "p13",
"p58", "p59", "p42", "p60", "p43", "p46", "p57", "p47", "p55",
"p31", "p34", "p33", "p41", "p38", "p39", "p90", "p83", "p65",
"p76", "p61", "p62", "p78", "p70", "p85", "p82", "p87", "p88",
"p89", "p63", "p79", "p15", "p20", "p23", "p24", "p25", "p26",
"p3", "p5", "p7", "p8", "p9", "p10", "p11", "p13", "p14", "p34",
"p33", "p41", "p38", "p39", "p58", "p59", "p42", "p60", "p43",
"p46", "p57", "p47", "p55", "p31", "p82", "p85", "p65", "p76",
"p61", "p62", "p78", "p70", "p63", "p79", "p83", "p90", "p87",
"p88", "p89"), trat = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Controle", "Dano"), class = "factor"),
pair = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L,
25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L,
37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 1L, 2L, 3L,
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 43L, 44L, 45L), vol_p_ul = c(0.15, 0.13, 0, 0,
0.05, 0.3, 0.52, 0.19, 0.21, 0.02, 0.07, 0.11, 0, 0, 0.06,
0, 1.79, 0.21, 1.99, 3.23, 0.32, 0, 0.03, 3.94, 2.41, 5.46,
3.35, 1.83, 2.09, 4.75, 1.1, 0.31, 0.42, 1.43, 1.08, 0.24,
0.14, 1.23, 5.25, 0.67, 2.46, 0.35, 0.69, 0.49, 0.15, 0.11,
0.09, 0.07, 0, 0, 0, 0.13, 0, 0.42, 0.09, 0.03, 0.02, 0.07,
0.03, 0.05, 0, 2.33, 1.25, 0.69, 0.67, 0.13, 0.53, 0.02,
0.37, 1.78, 0.3, 0.02, 0.3, 0.74, 0.33, 0.59, 0.57, 0, 0.32,
0.51, 0.14, 0, 1.42, 0.2, 0.14, 0.38, 0, 0.36, 0, 0.24),
vol_f_ul = c(0.010714286, 0.01, 0, 0, 0.002173913, 0.015789474,
0.086666667, 0.027142857, 0.023333333, 0.002222222, 0.01,
0.008461538, 0, 0, 0.008571429, 0, 0.298333333, 0.019090909,
0.284285714, 0.538333333, 0.08, 0, 0.004285714, 0.4925, 0.241,
0.78, 0.67, 0.1525, 0.139333333, 0.527777778, 0.275, 0.062,
0.14, 0.089375, 0.063529412, 0.015, 0.01, 0.136666667, 0.22826087,
0.134, 0.091111111, 0.029166667, 0.115, 0.040833333, 0.011538462,
0.008461538, 0.006428571, 0.01, 0, 0, 0, 0.021666667, 0,
0.046666667, 0.01, 0.004285714, 0.001538462, 0.0175, 0.002,
0.007142857, 0, 0.466, 0.104166667, 0.046, 0.074444444, 0.010833333,
0.088333333, 0.001818182, 0.052857143, 0.296666667, 0.075,
0.003333333, 0.042857143, 0.0925, 0.033, 0.118, 0.024782609,
0, 0.02, 0.03, 0.00875, 0, 0.157777778, 0.016666667, 0.010769231,
0.076, 0, 0.013333333, 0, 0.04), brix = c(2.34, 5.34, NA,
NA, NA, 4.34, 2.34, 2.34, 3.84, NA, 8.84, 5.34, NA, NA, 11.34,
NA, 5.42, 3.42, 4.42, 4.92, 5.42, NA, NA, 7.42, 7.42, 6.42,
5.42, 4.42, 5.42, 5.42, 3.84, 3.84, 2.84, 4.34, 5.34, 4.34,
6.84, 8.84, 5.34, 3.34, 4.84, 5.34, 4.84, 7.34, 7.84, NA,
1.84, NA, NA, NA, NA, 1.84, NA, 2.84, 1.84, NA, NA, 2.84,
NA, NA, NA, 4.92, 6.42, 4.92, 4.42, NA, 3.42, NA, 4.42, 4.42,
3.92, NA, 2.42, 5.42, 1.92, 3.34, 10.34, NA, 2.34, 3.84,
3.34, NA, 5.34, 2.34, 4.84, 2.34, NA, 4.84, NA, 5.84), mg_acucar = c(0.024506123,
0.053963963, NA, NA, NA, 0.044027683, 0.024506123, 0.024506123,
0.039103418, NA, 0.089662318, 0.053963963, NA, NA, 0.116038643,
NA, 0.054763919, 0.034989639, 0.044818279, 0.049776474, 0.054763919,
NA, NA, 0.075006199, 0.075006199, 0.064826559, 0.054763919,
0.044818279, 0.054763919, 0.054763919, 0.039103418, 0.039103418,
0.029342638, 0.044027683, 0.053963963, 0.044027683, 0.069087758,
0.089662318, 0.053963963, 0.034208403, 0.048981198, 0.053963963,
0.048981198, 0.074187523, 0.079316538, NA, 0.019698858, NA,
NA, NA, NA, 0.019698858, NA, 0.029342638, 0.019698858, NA,
NA, 0.029342638, NA, NA, NA, 0.049776474, 0.064826559, 0.049776474,
0.044818279, NA, 0.034989639, NA, 0.044818279, 0.044818279,
0.039889334, NA, 0.025277999, 0.054763919, 0.020466054, 0.034208403,
0.105400363, NA, 0.024506123, 0.039103418, 0.034208403, NA,
0.053963963, 0.024506123, 0.048981198, 0.024506123, NA, 0.048981198,
NA, 0.058975978), mg_totais_p = c(0.003675918, 0.007015315,
NA, NA, NA, 0.013208305, 0.012743184, 0.004656163, 0.008211718,
NA, 0.006276362, 0.005936036, NA, NA, 0.006962319, NA, 0.098027416,
0.007347824, 0.089188376, 0.160778012, 0.017524454, NA, NA,
0.295524426, 0.180764941, 0.353953014, 0.18345913, 0.082017451,
0.114456592, 0.260128617, 0.043013759, 0.012122059, 0.012323908,
0.062959586, 0.05828108, 0.010566644, 0.009672286, 0.110284651,
0.283310804, 0.02291963, 0.120493746, 0.018887387, 0.033797026,
0.036351886, 0.011897481, NA, 0.001772897, NA, NA, NA, NA,
0.002560851, NA, 0.012323908, 0.001772897, NA, NA, 0.002053985,
NA, NA, NA, 0.115979185, 0.081033199, 0.034345767, 0.030028247,
NA, 0.018544509, NA, 0.016582763, 0.079776537, 0.0119668,
NA, 0.0075834, 0.0405253, 0.006753798, 0.020182958, 0.060078207,
NA, 0.007841959, 0.019942743, 0.004789176, NA, 0.076628827,
0.004901225, 0.006857368, 0.009312327, NA, 0.017633231, NA,
0.014154235), mg_totais_f = c(0.000262566, 0.00053964, NA,
NA, NA, 0.000695174, 0.002123864, 0.000665166, 0.000912413,
0.000896623, 0.000456618, NA, NA, NA, 0.000994617, NA, 0.016337903,
0.000667984, 0.012741197, 0.026796335, 0.004381114, NA, NA,
0.036940553, 0.018076494, 0.050564716, 0.036691826, 0.006834788,
0.007630439, 0.02890318, 0.01075344, 0.002424412, 0.004107969,
0.003934974, 0.003428299, 0.000660415, 0.000690878, 0.01225385,
0.012317861, 0.004583926, 0.004462731, 0.001573949, 0.005632838,
0.003029324, 0.000915191, NA, 0.000126636, NA, NA, NA, NA,
0.000426809, NA, 0.001369323, 0.000196989, NA, NA, 0.000513496,
NA, NA, NA, 0.023195837, 0.006752767, 0.002289718, 0.003336472,
NA, 0.003090751, NA, 0.002368966, 0.01329609, 0.0029917,
NA, 0.001083343, 0.005065663, 0.00067538, 0.004036592, 0.002612096,
NA, 0.000490122, 0.001173103, 0.000299324, NA, 0.008514314,
0.000408435, 0.00052749, 0.001862465, NA, 0.000653083, NA,
0.002359039)), row.names = c(NA, -90L), class = "data.frame")
ggplot(dat,aes(x= species, y= vol_p_ul, fill=trat)) +
geom_boxplot() +
geom_point(aes(fill=trat),position=position_jitterdodge(0.2), alpha=0.8)+
geom_line(aes(group = pair), alpha = 0.6, colour = "black")+
theme_bw() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(color="black", size=14),
axis.text.y = element_text(size = 14, color="black")) +
theme(axis.title = element_text(size = 14))
I agree with the other response's use of facet_wrap and geom_line, but my take is slightly different:
the geom_point geometry is necessary since otherwise you will only get dots for the outliers
add the geom_line and geom_point after geom_boxplot, so they don't get covered
jitter the position of the points, as the example does
ggplot(data = dat, aes(x = trat, y = vol_p_ul, fill = trat)) +
geom_boxplot() +
geom_line(aes(group = pair), position=position_dodge(0.2)) +
geom_point(aes(fill=trat,group=pair), position = position_dodge(0.2)) +
facet_wrap(~species)
Are you looking for something like this?:
ggplot(data = dat, aes(x = trat, y = vol_p_ul)) +
geom_line( aes(group = pair)) +
geom_boxplot(aes(fill = trat)) +
facet_wrap(~species) +
theme_bw() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(color="black", size=14),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 14, color="black")) +
theme(axis.title = element_text(size = 14))
Related
I have an issue that is related to this one, but was unable to come to a solution for mine.
I have a reactive ggplot that I would like to update using a check box based on group data.
Currently, when I have ONE box selected, the data displays correctly. If I select more than one check box, I lose data points. See pictures below. I think I have to change the way I'm filtering my data and use droplevels somewhere but not sure how to integrate that (I'm new to shiny!). Any suggestions are appreciated!
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
```
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
```
renderPlot({
f <- WHOC_Sum_CMJ %>%
select(Date, Athlete, JumpHeight_cm, Program)%>%
filter(Program == input$Program)
p <- ggplot(f)+
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete))+
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)")+
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0))+
guides(col = guide_legend(nrow = 3))+
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
The issue in your code indeed is based on the filter call. You'll need to use %in%instead of ==, when filtering a vector of statements. Please see the following:
---
title: "Test"
output: flexdashboard::flex_dashboard
runtime: shiny
---
```{r global, include=FALSE}
library(ggplot2)
library(dplyr)
library(scales)
library(ggrepel)
WHOC_Sum_CMJ <- structure(list(Athlete = structure(c(1L, 1L, 1L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 14L, 14L,
14L, 14L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L), .Label = c("Athlete 1", "Athlete 10",
"Athlete 11", "Athlete 12", "Athlete 13", "Athlete 14", "Athlete 2",
"Athlete 3", "Athlete 4", "Athlete 5", "Athlete 6", "Athlete 7",
"Athlete 8", "Athlete 9"), class = "factor"), Date = structure(c(1L,
4L, 5L, 1L, 3L, 5L, 7L, 2L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 6L, 7L, 2L, 4L, 5L, 8L, 1L, 3L, 5L, 7L, 1L, 3L,
5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L, 5L, 7L, 1L, 3L,
6L, 7L, 1L, 3L, 5L, 7L), .Label = c("2020-01-06", "2020-01-07",
"2020-01-13", "2020-01-14", "2020-01-21", "2020-01-23", "2020-01-27",
"2020-01-28"), class = "factor"), Position = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("DEF", "FWD", "GOALIE"), class = "factor"),
Program = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L,
4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("Navy", "Red", "RTP", "White"), class = "factor"),
mRSI = c(0.36, 0.38, 0.42, 0.46, 0.46, 0.47, 0.48, 0.31,
0.3, 0.24, 0.3, 0.29, 0.26, 0.28, 0.28, 0.36, 0.35, 0.43,
0.43, 0.28, 0.31, 0.28, 0.3, 0.33, 0.36, 0.35, 0.37, 0.37,
0.36, 0.37, 0.36, 0.3, 0.36, 0.34, 0.37, 0.26, 0.28, 0.34,
0.3, 0.39, 0.4, 0.43, 0.43, 0.43, 0.47, 0.46, 0.48, 0.34,
0.36, 0.33, 0.37, 0.28, 0.28, 0.34, 0.33), SystemWeight = c(617.21,
612.4, 620.45, 672.08, 682.23, 670.5, 663.41, 517.33, 515.23,
511.62, 517.85, 697.55, 703.92, 689.43, 691.33, 859.06, 845.9,
850.97, 851.84, 655.79, 665.09, 673.91, 667.92, 626.78, 632.92,
634.52, 624.88, 637.55, 645.6, 648.78, 646.64, 558.03, 563.23,
569.58, 560.95, 693.63, 695.54, 684.37, 684.58, 641.18, 660.8,
663.95, 660, 594.92, 596.97, 591.36, 585.64, 522.35, 518.17,
530.95, 523.5, 780.65, 789.81, 775.84, 775.48), FTCT = c(0.61,
0.62, 0.67, 0.74, 0.75, 0.77, 0.77, 0.54, 0.55, 0.44, 0.53,
0.53, 0.49, 0.53, 0.56, 0.6, 0.58, 0.68, 0.68, 0.53, 0.57,
0.54, 0.55, 0.61, 0.63, 0.64, 0.65, 0.59, 0.58, 0.59, 0.59,
0.51, 0.59, 0.59, 0.59, 0.53, 0.57, 0.63, 0.59, 0.76, 0.76,
0.79, 0.78, 0.67, 0.72, 0.72, 0.74, 0.63, 0.65, 0.61, 0.63,
0.49, 0.5, 0.53, 0.57), JumpHeight_cm = c(28.97, 29.78, 31.43,
35.83, 35.41, 36.59, 36.92, 27.56, 26.11, 26.15, 26.82, 26.15,
25.08, 24.98, 24.62, 29.39, 30.17, 32.42, 32.56, 26.6, 27.25,
25.58, 27.88, 29.17, 31.58, 28.48, 31.24, 33.73, 32.78, 33.09,
33.43, 29.73, 31.91, 30.65, 32.98, 24.15, 24.24, 27.57, 25.44,
26.68, 26.39, 27.43, 28.87, 35.44, 36.29, 35.71, 36.06, 26.79,
27.76, 26.82, 29.71, 28.69, 26.9, 31.12, 29.77), EJH = c(17.6,
18.58, 21.11, 26.66, 26.69, 28.08, 28.38, 14.99, 14.39, 11.41,
14.33, 13.8, 12.34, 13.29, 13.67, 17.58, 17.5, 22.03, 22.19,
14.03, 15.59, 13.92, 15.39, 17.7, 19.75, 18.37, 20.3, 19.99,
18.9, 19.62, 19.61, 15.09, 18.8, 18.18, 19.6, 12.78, 13.87,
17.28, 15.06, 20.44, 20.12, 21.74, 22.52, 23.8, 26.25, 25.68,
26.73, 16.99, 18.13, 16.42, 18.82, 14.09, 13.43, 16.61, 16.9
), Weight = c(62.94, 62.45, 63.27, 68.54, 69.57, 68.38, 67.65,
52.76, 52.54, 52.17, 52.81, 71.13, 71.78, 70.31, 70.5, 87.61,
86.26, 86.78, 86.87, 66.88, 67.82, 68.72, 68.11, 63.92, 64.54,
64.71, 63.72, 65.02, 65.84, 66.16, 65.94, 56.91, 57.44, 58.09,
57.2, 70.74, 70.93, 69.79, 69.81, 65.39, 67.39, 67.71, 67.31,
60.67, 60.88, 60.31, 59.72, 53.27, 52.84, 54.15, 53.39, 79.61,
80.54, 79.12, 79.08)), class = "data.frame", row.names = c(NA,
-55L))
WHOC_Sum_CMJ$Date <- as.Date(WHOC_Sum_CMJ$Date)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
checkboxGroupInput("Program", label = "Program", choices = unique(WHOC_Sum_CMJ$Program), selected = "Red", inline = TRUE)
# (Note: for the code I cut out some of the styling to make it more readable. That's why it looks different than the pictures).
```
Column
-----------------------------------------------------------------------
```{r}
renderPlot({
f <- WHOC_Sum_CMJ %>%
dplyr::select(Date, Athlete, JumpHeight_cm, Program) %>%
filter(Program %in% input$Program)
p <- ggplot(f) +
geom_line(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
geom_point(aes(x=Date, y=JumpHeight_cm, colour = Athlete)) +
theme_bw() +
labs(title = "Team Jump Height",
x = "Date",
y = "Jump Height (cm)") +
scale_x_date(limits = c(min = min(WHOC_Sum_CMJ$Date), max = max(WHOC_Sum_CMJ$Date)), labels = date_format("%m/%d"),
date_breaks = "2 weeks", expand = c(.08,0)) +
guides(col = guide_legend(nrow = 3)) +
geom_text_repel(data= subset(f, Date == min(Date)), aes(x=Date, y=JumpHeight_cm,label = unique(Athlete)),
force = .1,
nudge_x = -2,
direction = "y",
hjust = 1,
)
p
})
```
I have two plots: a barplot, and a ggplot(geom_jitter bubbleplot). Ultimately, I am using a photo editing app to line up these two plots. As you can see, the intervals in the bottom of these two plots do not match up, which is my problem here. I would like to make it so I can just change the bottom x-axis of both plots to 400 (lowest common interval to cover x-axis of both plots). I do not want to change the data values, just the axis values.
Barplot Code
GYPCdomain <- read.csv(file.choose(), header=TRUE)
GYPCbarplot <- barplot(as.matrix(GYPCdomain), horiz=TRUE, xlab = "Length (Protein Domains Shown)",
col=c("azure", "plum1", "skyblue"),
legend = c("Cytoplasmic", "Helical Membrane", "Extracellular"))
sample data:
structure(list(GYPC = c(0L, 0L, 171L, 0L, 72L, 0L, 141L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L)), class = "data.frame", row.names = c(NA, -42L))
Bubbleplot Code
library(ggplot2)
library(scales)
data(GYPC, package="ggplot2")
GYPC <- read.csv(file.choose(), header = TRUE)
GYPCggplot <- ggplot(GYPC, aes(Position, log10(Frequency)))+
geom_jitter(aes(col=Geographical.Location, size =(p.value)))+
labs(subtitle="Frequency of Various Polymorphisms", title="GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") + labs(x = "Position of Polymorphism on GYPC Gene") +
scale_size_continuous(range=c(1,4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))
sample data:
structure(list(Variant = structure(c(4L, 4L, 4L, 4L, 4L, 8L,
8L, 8L, 8L, 8L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 12L,
12L, 12L, 12L, 12L, 14L, 14L, 14L, 14L, 14L, 2L, 2L, 2L, 2L,
2L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 13L, 13L, 13L, 13L, 13L, 15L, 15L, 15L, 15L, 15L), .Label = c("rs111631066",
"rs114199197", "rs115178969", "rs115201071", "rs139780142", "rs139816143",
"rs143080607", "rs143216051", "rs199797395", "rs531807314", "rs545780841",
"rs551011574", "rs560942282", "rs567759380", "rs571586275"), class = "factor"),
Position = c(213L, 213L, 213L, 213L, 213L, 60L, 60L, 60L,
60L, 60L, 249L, 249L, 249L, 249L, 249L, 183L, 183L, 183L,
183L, 183L, 282L, 282L, 282L, 282L, 282L, 294L, 294L, 294L,
294L, 294L, 150L, 150L, 150L, 150L, 150L, 135L, 135L, 135L,
135L, 135L, 258L, 258L, 258L, 258L, 258L, 255L, 255L, 255L,
255L, 255L, 138L, 138L, 138L, 138L, 138L, 159L, 159L, 159L,
159L, 159L, 141L, 141L, 141L, 141L, 141L, 198L, 198L, 198L,
198L, 198L, 258L, 258L, 258L, 258L, 258L), Geographical.Location = structure(c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("AFR",
"AMR", "EAS", "EUR", "SAS"), class = "factor"), Frequency = c(0.023,
0.001, 0, 0, 0, 0.017, 0.001, 0, 0, 0, 0.012, 0, 0, 0, 0,
0.002, 0.003, 0.002, 0.023, 0.016, 0.001, 0, 0, 0, 0, 0,
0, 0, 0, 0.004, 0, 0, 0, 0.001, 0, 0, 0, 0, 0, 0.001, 0,
0, 0.001, 0, 0, 0.001, 0, 0, 0, 0, 0, 0.001, 0, 0, 0, 0,
0, 0, 0, 0.002, 0, 0, 0.001, 0, 0, 0, 0, 0, 0, 0.001, 0,
0, 0.001, 0, 0), pre.p.value = c(6.32e-17, 0.113, 0.00126,
0.00126, 0.00211, 2.51e-12, 0.356, 0.00806, 0.00809, 0.0139,
4.86e-10, 0.15, 0.0542, 0.0542, 0.0537, 0.000376, 0.0778,
0.0068, 7.4e-06, 0.0109, 0.264, 1, 1, 1, 1, 0.579, 1, 0.589,
0.59, 0.00144, 1, 1, 1, 0.201, 1, 1, 1, 1, 1, 0.195, 1, 1,
0.201, 1, 1, 1, 1, 0.201, 1, 1, 1, 0.139, 1, 1, 1, 1, 1,
1, 1, 0.0381, 1, 1, 0.201, 1, 1, 1, 1, 1, 1, 0.195, 1, 1,
0.201, 1, 1), p.value = c(0, 0.75, 0.5, 0.5, 0.5, 0, 0.75,
0.5, 0.5, 0.75, 0, 0.75, 0.75, 0.75, 0.75, 0.5, 0.75, 0.5,
0.25, 0.75, 0.75, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 1, 1, 1, 0.75,
1, 1, 1, 1, 1, 0.75, 1, 1, 0.75, 1, 1, 1, 1, 0.75, 1, 1,
1, 0.75, 1, 1, 1, 1, 1, 1, 1, 0.75, 1, 1, 0.75, 1, 1, 1,
1, 1, 1, 0.75, 1, 1, 0.75, 1, 1), log.p.value. = c(-16.19928292,
-0.947, -2.899629455, -2.899629455, -2.675717545, -11.60032628,
-0.449, -2.093664958, -2.092051478, -1.8569852, -9.313363731,
-0.824, -1.266000713, -1.266000713, -1.270025714, -3.424812155,
-1.11, -2.167491087, -5.13076828, -1.962573502, -0.5783960731,
0, 0, 0, 0, -0.2373214363, 0, -0.2298847052, -0.2291479884,
-2.841637508, 0, 0, 0, -0.6968039426, 0, 0, 0, 0, 0, -0.7099653886,
0, 0, -0.6968039426, 0, 0, 0, 0, -0.6968039426, 0, 0, 0,
-0.857, 0, 0, 0, 0, 0, 0, 0, -1.419075024, 0, 0, -0.6968039426,
0, 0, 0, 0, 0, 0, -0.7099653886, 0, 0, -0.6968039426, 0,
0), X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), range = structure(c(2L, 6L, 5L, 4L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "0 < p-value < 1E-9",
"1E-2 < p-value < 1", "1E-4 < p-value < 1E-2", "1E-6 < p-value < 1E-4",
"1E-9 < p-value < 1E-6"), class = "factor")), class = "data.frame", row.names = c(NA,
-75L))
I took the liberty to produce your barplot also with ggplot, because than we can use the awesome features of the cowplot package, which was made for things like these. Setting axis limits can be done with ylim() or xlim() but because of different width of the legends, we need the cowplot package to truly align the plots (or the legends would need to go below the plots)
#recreating the barplot
library(dplyr) #needed for data wrangling
GYPCbarplot_ggplot=GYPCdomain %>%
filter(GYPC>0) %>%
mutate(domain=factor(c("Cytoplasmic", "Helical Membrane", "Extracellular"),
levels=c("Cytoplasmic", "Helical Membrane", "Extracellular"),
ordered = T)) %>%
ggplot(aes(x=1,y=GYPC,fill=domain))+
geom_col(position="stack")+
scale_fill_manual(values=c("Cytoplasmic"="azure", "Helical Membrane"="plum1", "Extracellular"="skyblue"))+
coord_flip()+
xlab("GYPC")+
ylab( "Length (Protein Domains Shown)")+
ylim(0,400)+ #creates the limit
theme(panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
#the bubbleplot
GYPC_bubbleplot <- ggplot(GYPC_data, aes(Position, log10(Frequency)))+
geom_jitter(aes(col=Geographical.Location, size =(p.value)))+
labs(subtitle="Frequency of Various Polymorphisms", title="GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") + labs(x = "Position of Polymorphism on GYPC Gene") +
scale_size_continuous(range=c(1,4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))+
xlim(0,400) #added this limit
library(cowplot) #used to arrange the two plots
plot_grid(GYPCbarplot_ggplot,GYPC_bubbleplot,
ncol = 1, #both plots in one column (below each other)
align = "v", #align both bottom axes
rel_heights = c(1,1.5) #make bottom plot a bit higher
)
et voila:
If I understand correctly, the OP is asking to synchronise the x-axes in order to show the protein domains a certain position on the GYPC gene belongs to.
If my assumption is correct then there is an alternative approach which fills the background of the bubble plot according to the protein domains:
library(dplyr)
domain_name <- c("Cytoplasmic", "Helical Membrane", "Extracellular")
domain_fill <- c("azure", "plum1", "skyblue")
names(domain_fill) <- domain_name
GPYCdomain_2 <- GYPCdomain %>%
filter(GYPC > 0) %>%
mutate(domain_name = forcats::fct_inorder(rev(domain_name)),
end_pos = cumsum(GYPC),
start_pos = lag(end_pos, default = 0L))
library(ggplot2)
ggplot(GYPC, aes(Position, log10(Frequency))) +
geom_rect(aes(xmin = start_pos, xmax = end_pos, ymin = -Inf, ymax = Inf, fill = domain_name),
data = GPYCdomain_2, inherit.aes = FALSE, alpha = 0.6) +
scale_fill_manual(values = domain_fill) +
geom_jitter(aes(color = Geographical.Location, size = (p.value))) +
labs(subtitle = "Frequency of Various Polymorphisms", title = "GYPC Gene") +
labs(color = "Geographical Location") +
labs(size = "p-value") +
labs(x = "Position of Polymorphism on GYPC Gene") +
labs(fill = "Protein Domain") +
scale_size_continuous(range = c(1, 4.5), trans = "reverse") +
guides(size = guide_legend(reverse = TRUE))
Data Sets
> dput(head(spdistbc,50))
structure(list(Lane = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), Vehicle.class = c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
speedmph = c(0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 40.8, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4,
23.8, 27.2, 30.6, 34, 37.4, 40.8, 3.4, 6.8, 10.2, 13.6, 17,
20.4, 23.8, 27.2, 30.6, 34, 37.4, 40.8, 0, 3.4, 6.8, 10.2,
13.6, 17, 20.4, 23.8, 27.2, 30.6, 34, 37.4), cprob = c(0,
0, 0.03, 0.06, 0.11, 0.2, 0.28, 0.43, 0.56, 0.75, 0.91, 0.97,
1, 0, 0, 0.01, 0.01, 0.02, 0.05, 0.17, 0.36, 0.57, 0.76,
0.93, 0.99, 1, 0, 0.01, 0.01, 0.04, 0.07, 0.16, 0.32, 0.55,
0.76, 0.94, 0.99, 1, 0, 0, 0, 0.01, 0.03, 0.06, 0.11, 0.25,
0.47, 0.74, 0.92, 0.98)), .Names = c("Lane", "Vehicle.class",
"speedmph", "cprob"), row.names = c(7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 66L, 67L, 68L, 69L, 70L, 71L, 72L,
73L, 74L, 75L, 76L, 77L), class = "data.frame")
> dput(head(cspdistbv,50))
structure(list(lanem = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L,
11L, 11L), cars = structure(c(34, 35, 36, 37, 38, 39, 40, 24,
26, 28, 30, 32, 34, 36, 38, 40, 20, 25, 30, 35, 40, 10, 15, 20,
25, 30, 35, 40, 10, 15, 20, 25, 30, 35, 40, 35, 40, 45, 50, 55,
0, 0.03, 0.07, 0.17, 0.67, 0.93, 1, 0, 0.03, 0.1, 0.1, 0.2, 0.27,
0.33, 0.8, 1, 0, 0.1, 0.31, 0.52, 1, 0, 0.07, 0.27, 0.37, 0.5,
0.77, 1, 0, 0.03, 0.07, 0.23, 0.4, 0.77, 1, 0, 0.13, 0.47, 0.77,
1), .Dim = c(40L, 2L), .Dimnames = list(NULL, c("speedmph", "prob"
)))), .Names = c("lanem", "cars"), row.names = c(NA, 40L), class = "data.frame")
Problem
I created the plot using spdistbc:
cb1 <- ggplot() + geom_point(data = spdistbc, mapping = aes(x=speedmph, y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw() + my.theme()
Which gave me this:
But when I combine another plot from the second data frame using following code:
cb2 <- cb1 + geom_point(data = cspdistbv, mapping = aes(x = cars.speedmph, y = cars.prob, color = 'simulated-default')) + facet_wrap(~lanem)
I get the error:
Error in eval(expr, envir, enclos) : object 'cars.speedmph' not found
Question
You can see in the cspdistbv data frame, there is a column named cars.speedmph, then why R can't find it? Please help.
Somehow you've created an invalid data.frame. You've stored a matrix in the second column of cspdistbv; dim(cspdistbv) thinks it only has two columns and this interferes with proper naming and such. I'm not sure how you created it, but you can fix it with
cspdistbv <- cbind.data.frame(lanem=cspdistbv[,1], cspdistbv[,2])
And then
cb1 <- ggplot() + geom_point(data = spdistbc, mapping = aes(x=speedmph,
y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw()
cb2 <- cb1 + geom_point(data = cspdistbv, mapping = aes(x = speedmph,
y = prob, color = 'simulated-default')) + facet_wrap(~lanem)
should work
Data Sets
> dput(head(spdistuc,50))
structure(list(Lane = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), Vehicle.class = c(2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
speedmph = c(0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2,
30.6, 34, 37.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6,
34, 0, 3.4, 6.8, 10.2, 13.6, 17, 20.4, 23.8, 27.2, 30.6,
34, 37.4, 0, 3.4, 6.8, 10.2, 13.6, 17), cprob = c(0, 0.01,
0.04, 0.08, 0.14, 0.22, 0.32, 0.5, 0.73, 0.95, 0.99, 1, 0,
0, 0.03, 0.07, 0.16, 0.3, 0.51, 0.81, 0.99, 1, 1, 0, 0.03,
0.05, 0.1, 0.21, 0.49, 0.84, 1, 1, 0, 0, 0.01, 0.01, 0.06,
0.1, 0.17, 0.4, 0.76, 0.95, 1, 1, 0, 0, 0.01, 0.01, 0.02,
0.04)), .Names = c("Lane", "Vehicle.class", "speedmph", "cprob"
), row.names = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L,
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 88L, 89L, 90L, 91L, 92L,
93L), class = "data.frame")
> dput(head(cspdistuv,50))
structure(list(lanem = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), cars.speedmph = c(18,
20, 22, 24, 26, 28, 30, 32, 34, 36, 10, 15, 20, 25, 30, 35, 5,
10, 15, 20, 25, 30, 35, 0, 5, 10, 15, 20, 25, 30, 35, 5, 10,
15, 20, 25, 30, 35), cars.prob = c(0, 0.13, 0.17, 0.2, 0.2, 0.27,
0.37, 0.8, 0.97, 1, 0, 0.03, 0.13, 0.4, 0.77, 1, 0, 0.03, 0.17,
0.27, 0.5, 0.8, 1, 0, 0.03, 0.1, 0.27, 0.53, 0.6, 0.83, 1, 0,
0.07, 0.17, 0.33, 0.53, 0.8, 1)), .Names = c("lanem", "cars.speedmph",
"cars.prob"), row.names = c(NA, 38L), class = "data.frame")
Problem
I plotted the spdistuc:
cu1 <- ggplot() + geom_point(data = spdistuc, mapping = aes(x=speedmph, y = cprob, color = 'observed')) + facet_wrap(~Lane) + theme_bw() + my.theme()
This gave me following:
But when I added another plot on the existing one,
cu2 <- cu1 + geom_point(data = cspdistuv, mapping = aes(x = cars.speedmph, y = cars.prob, color = 'simulated-default')) + facet_wrap(~lanem)
I got the following:
Question
Why the existing plot ("observed") changed? You can see more than 1 point for a single value on x-axis. What am I doing wrong?
Expanding my comment into an answer:
The problem is you use "Lane" in the first dataset and "lanem" in the second.
This can be fixed by making the column names the same.
names(cspdistuv)[names(cspdistuv) == "lanem"] <- "Lane"
When this change is made, you should not need to include facet_wrap in your cu2 definition. It will still be remembered from cu1's definition.
As similar question has been asked before (here) but I can't adjust the solution provided there to my specific problem. For every plot shown below, there should be a x as well as y-axis.
The data:
dput(df_nSubj)
structure(list(nSubj = c(10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L,
40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 40L, 50L, 50L, 50L,
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L,
50L, 50L, 50L, 50L, 50L, 50L, 50L, 50L), family = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("AOV", "MLM1", "MLM2",
"MLM3"), class = "factor"), Spher = structure(c(1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L), .Label = c("met", "vio"), class = "factor"),
effSize = c(0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8, 0.2, 0.5, 0.8,
0.2, 0.5, 0.8), pow = c(0.12, 0.53, 0.84, 0.1, 0.4, 0.74,
0.13, 0.55, 0.84, 0.11, 0.44, 0.74, 0.12, 0.5, 0.82, 0.1,
0.43, 0.76, 0.12, 0.49, 0.81, 0.1, 0.43, 0.76, 0.2, 0.84,
0.99, 0.15, 0.72, 0.97, 0.21, 0.81, 0.98, 0.17, 0.69, 0.95,
0.2, 0.83, 0.99, 0.17, 0.75, 0.97, 0.19, 0.82, 0.99, 0.17,
0.75, 0.97, 0.32, 0.95, 1, 0.23, 0.87, 1, 0.32, 0.92, 1,
0.25, 0.83, 0.99, 0.3, 0.94, 1, 0.24, 0.89, 1, 0.3, 0.94,
1, 0.24, 0.89, 1, 0.41, 0.99, 1, 0.29, 0.96, 1, 0.4, 0.97,
1, 0.3, 0.92, 1, 0.38, 0.99, 1, 0.32, 0.97, 1, 0.37, 0.98,
1, 0.32, 0.97, 1, 0.5, 1, 1, 0.36, 0.98, 1, 0.47, 0.99, 1,
0.36, 0.96, 1, 0.47, 1, 1, 0.4, 0.99, 1, 0.46, 1, 1, 0.4,
0.99, 1)), class = "data.frame", .Names = c("nSubj", "family",
"Spher", "effSize", "pow"), row.names = c(NA, -120L))
plot:
require(ggplot2)
require(grid)
pl1 <- ggplot(data=df_nSubj,aes(x=nSubj,y=pow,group=family))+
geom_point(aes(shape=family))+geom_line()+
labs(x="Number of subjects",y="Power",shape="")+
scale_y_continuous(limits=c(0.2,1),breaks=c(0.2,0.4,0.6,0.8,1))+
guides(shape = guide_legend(ncol = 4))+
facet_grid(Spher~effSize)+
theme_bw()+
theme(legend.position = "top",
panel.margin = unit(2, "lines"),
legend.key = element_blank(),
strip.text.x = element_blank(),
strip.text.y = element_blank(),
strip.background = element_blank(),
panel.border=element_blank(),
axis.line=element_line(),
axis.title.x = element_text(vjust=-0.5))
Thanks in advance
Change facet_grid(Spher~effSize) to facet_wrap(Spher~effSize, scales = "free")
I'm late to this party, but I have two workarounds currently.
The first option is to add horizontal and/or vertical line geoms with intercepts set to -Inf, which requires turning off plot clipping. To the original plot, add the following lines:
geom_hline(aes(yintercept=-Inf)) +
geom_vline(aes(xintercept=-Inf)) +
coord_cartesian(clip="off")
Fake Axis Option
Alternatively, use the lemon package and its facet command. This will reproduce the axes and their ticks without adding the labels.
library(lemon)
And change facet_grid(Spher~effSize) to facet_rep_grid(Spher~effSize)
Lemon Package Option
The ggh4x package also has this functionality via its facet_wrap2 function