rggplot2shapesposition-dodge

Combine 2 variables in scatterplot without dodge


I want to create a scatterplot that combines both days (in color) as observer (shape) in a scatterplot. However, with my current code the points will not only dodge on day (which I want), but also on observer (which i dont want). Here is my dataframe

structure(list(sample = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 1, 2, 3, 
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
45, 46, 47, 48), treatment = c("a", "a", "a", "a", "a", "a", 
"b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", 
"d", "d", "d", "d", "d", "a", "a", "a", "a", "a", "a", "b", "b", 
"b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d", 
"d", "d", "d", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", 
"b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", 
"d", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", 
"c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", "d"), 
    day = structure(c(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, 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), levels = c("2", 
    "6"), class = "factor"), observer = c("OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", 
    "OBS1", "OBS1", "OBS1", "OBS1", "OBS1", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", "OBS2", 
    "OBS2", "OBS2", "OBS2", "OBS2", "OBS2"), count = c(1.94000836127381, 
    2.07418521661429, 0.812685661255674, 0.199532997122114, 0.720956738045624, 
    2.25080298569014, 1.1066027850052, 4.1487948134003, 5.06163333946851, 
    8.45581635201957, 1.39519183814535, 5.22744057847467, 77.578763434025, 
    81.5688787451947, 57.7998831807876, 72.5246292216229, 53.7941684202605, 
    18.1902377363129, 7.2040245328528, 18.7399963681316, 12.0408266827075, 
    16.9381875648501, 4.94300230430152, 7.7656112238281, 0.426353706529969, 
    1.58200029160696, 0.546681932009987, 0.100208840148698, 0.444814277410285, 
    1.61429066532657, 0.927880542016121, 0.0246114786607196, 
    2.28109051907718, 4.06940300139814, 1.36067523536742, 2.75085205976752, 
    27.7361069969966, 45.1989664871625, 32.2395897650876, 69.6007356941801, 
    4.34439825647985, 2.21082387173822, 2.40563040116718, 6.19711117695116, 
    0.190884751182731, 4.99307548525574, 2.27313622832564, 6.45085822279649, 
    3.62337602915357, 9.29131381820146, 17.3474341955159, 0.654156425021601, 
    18.2284156894217, 8.1096329723588, 2.59461805212543, 11.635214608248, 
    10.338591963394, 17.096512619713, 2.29518753169706, 13.6312931040208, 
    2.40586814654832, 18.2260582559852, 0.813121453291961, 86.1680580200406, 
    86.1245441941217, 75.7365169486812, 51.2171310942499, 62.4301976210013, 
    38.1836807429795, 31.2339903053221, 7.92988025761869, 8.27444313173916, 
    3.52366209108204, 2.79169044391839, 3.868126588607, 0.563527045569289, 
    15.1194456070968, 4.28505182586694, 0.825225268886448, 8.24445472788982, 
    2.5334457992282, 3.73731910584408, 1.48367595357901, 1.40416105449322, 
    1.2076803985627, 12.5666113109268, 0.523344806332173, 36.4416339714133, 
    16.9721558868451, 43.7864681289392, 39.3637375577211, 4.59273514657034, 
    33.3793562892502, 27.6940745715846, 3.93496345638513, 2.57838139836976
    ), length = c(556, 304, 878, 457, 271, 176, 814, 362, 353, 
    411, 132, 305, 502, 187, 915, 132, 430, 879, 673, 361, 447, 
    956, 920, 327, 591, 502, 659, 781, 624, 833, 484, 387, 486, 
    853, 114, 739, 618, 228, 677, 925, 460, 645, 353, 987, 315, 
    113, 333, 449, 808, 356, 590, 753, 361, 868, 878, 995, 644, 
    149, 651, 719, 911, 550, 882, 879, 784, 490, 573, 889, 992, 
    459, 849, 541, 680, 291, 699, 506, 678, 327, 194, 198, 506, 
    926, 472, 124, 916, 447, 385, 685, 677, 397, 953, 579, 458, 
    107, 142, 207), yield = c(3, 10, 9, 9, 9, 2, 5, 5, 4, 10, 
    3, 2, 4, 7, 6, 4, 6, 8, 7, 9, 6, 8, 4, 10, 6, 8, 5, 8, 2, 
    4, 8, 8, 8, 6, 3, 5, 9, 5, 2, 9, 7, 5, 8, 2, 4, 3, 2, 2, 
    3, 10, 5, 8, 4, 4, 8, 8, 10, 3, 5, 4, 10, 3, 7, 7, 10, 10, 
    9, 2, 6, 5, 10, 10, 8, 2, 2, 4, 4, 2, 6, 2, 5, 7, 10, 8, 
    8, 3, 6, 3, 9, 9, 9, 4, 5, 5, 3, 8)), row.names = c(NA, -96L
), class = c("tbl_df", "tbl", "data.frame"))

I can create a scatterplot as followed

final_data$day <- as.factor(final_data$day)

p <- ggplot(data = final_data)+
  aes(x = treatment, y = count, color = day)+
 geom_point(size = 2, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))

plot(p)

Graph 1

But now I only want to change the shape of my points (per observer), but I don't want the extra dodge for these points. This is what happens when I change my code.

final_data$day <- as.factor(final_data$day)

p <- ggplot(data = final_data)+
  aes(x = treatment, y = count, color = day)+
 geom_point(aes(shape = observer), size = 2, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))

plot(p)

Graph 2

How can I keep the layout from Layout 1, but with the different shape for the "observer". In other words, only dodge for days, not for observers.

I tried several codes, but nothing works for this. Hope you can help.

Thanks M


Solution

  • You have to explicitly set the group aes, i.e. use group=day to dodge the points by day only:

    library(ggplot2)
    
    ggplot(data = final_data) +
      aes(x = treatment, y = count, color = day, group = day) +
      geom_point(aes(shape = observer),
        size = 2,
        position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1)
      )
    

    enter image description here