Reuben Newton Addison
Reuben Newton Addison

Reputation: 361

ggplot with error bars differentiated by shape of points

Getting an error message that reads ' Error: All unnamed arguments must be length 1'. I am trying to differentiate my line by the shape of the point and I keep getting the errors above. I want to use all solid lines but differentiated by shape. The journal I want to public my work requires black and white instead of color

The plot I want to modify: The plot I want to modify

tgc <- structure(
            list(
                GROUP = 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), 
                            .Label = c("LLL", "LRL", "RLR", "RRR"), 
                            class = "factor"
                ), 
                condition = structure(
                                c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), 
                                .Label = c("Midline", "No Midline crossing", "Midline crossing"), 
                                class = "factor"
                ), 
                names = structure(
                            c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), 
                            .Label = c("102", "104"), 
                            class = "factor"
                ), 
                Trial_type = structure(
                                c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), 
                                .Label = c("retention", "transfer"), 
                                class = "factor"
                ), 
                Training = structure(
                                c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), 
                                .Label = c("right", "left"), 
                                class = "factor"
                ), 
                N = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8), 
                NormalizedJerk = c(2060.1571177375, 1092.701687475, 
                                    2981.812368875, 1508.28547575, 2089.925186675, 1269.6704558875, 
                                    1391.97364445, 914.38364425, 3900.4119165625, 2335.7186722875, 
                                    4015.516784, 2563.16723475, 1411.2016892375, 847.952527975, 
                                    1853.413925525, 1394.847246875, 6830.81906275, 3915.019566375, 
                                    2310.5893110125, 1023.1702538875, 1340.4653657625, 765.2752940875, 
                                    5617.967587, 1705.629421875
                ), 
                sd = c(1507.9737264907, 946.522319997832, 
                        1403.37144167604, 813.034284948276, 1383.95826055979, 915.414811222361, 
                        580.358119711544, 361.141583497209, 3283.59638643298, 1471.60790379469, 
                        1178.5979495842, 806.56121914288, 701.001824354642, 324.415375522303, 
                        2103.42765701483, 1208.14874080532, 4671.80701687463, 2861.85154237875, 
                        2130.92970547315, 813.649686635084, 489.544827982279, 256.250905151245, 
                        5305.91963495324, 878.475841087918
                ), 
                se = c(533.14922392636, 
                        334.646175507445, 496.166731466336, 287.451028112041, 489.303135460484, 
                        323.648010306967, 205.187580982353, 127.682831329662, 1160.9266357632, 
                        520.291964010475, 416.697301221774, 285.16245374901, 247.841571812654, 
                        114.6981559765, 743.673980005258, 427.145083652716, 1651.73321101347, 
                        1011.8173161826, 753.397422485958, 287.668605464989, 173.080233780536, 
                        90.5983763588182, 1875.92587715314, 310.588112170911
                ), 
                ci = c(1260.69758461414, 
                        791.31246230768, 1173.24788605882, 679.713672219011, 1157.01806049025, 
                        765.305934151606, 485.191530116531, 301.92191947415, 2745.15527724569, 
                        1230.29499600801, 985.332544042271, 674.302053778614, 586.052191261077, 
                        271.218041235005, 1758.50952839896, 1010.03762375384, 3905.72840792368, 
                        2392.56776402689, 1781.50181629779, 680.228160904389, 409.269718268804, 
                        214.231117892992, 4435.85982330679, 734.424182295757
                )
            ), 
            row.names = c(NA, -24L), 
            class = "data.frame"
         )


tgc <- summarySE(
            data10, 
            measurevar = "NormalizedJerk", 
            groupvars = c("GROUP", "condition", "names","Trial_type", "Training")
        )

pd <- position_dodge2(0.2)

p <-ggplot(
        gc, 
        aes(
            names, 
            NormalizedJerk, 
            group = interaction(Training, Trial_type), 
            color = interaction(Training, Trial_type), 
            linetype = interaction(Training, Trial_type), 
            shape = Training
        )
    ) +  
    geom_errorbar(
        aes(ymin = NormalizedJerk - se, ymax = NormalizedJerk + se),  
        width = .3,
        position = pd
    ) +
    geom_line(
        position = pd, 
        size = 1
    ) + 
    geom_point(
            aes(shape = Training), 
            position = pd, 
            size = 1
    )+  
    scale_colour_manual(
        name = "Experimental group",
        values = c("#999999","#999999","#000000","#000000")
        labels = c("RRR","LLL","LRL","RLR")
    ) +  
    scale_shape_manual(
        name = "Experimental group",
        values = c("19","18","19","18")
    ) +
    scale_linetype_manual(
        name = "Experimental group",
        values = c("solid","solid","solid","solid"),
        labels = c("RRR","LLL","LRL","RLR")
    ) + 
    theme_bw() +  
    facet_wrap(.~condition) + 
    theme(
        axis.title.y = element_text(vjust= 1.8, size = 14),
        axis.title.x = element_text(vjust= -0.5, size = 14),
        axis.title = element_text(face = "bold")
    ) +  
    xlab("Block of trials") + 
    ylab("Normalized Jerk") 

    p + 

    scale_x_discrete(
        breaks=c("102","104"), 
        labels=c("Pretest","Posttest")
    )

Upvotes: 1

Views: 1171

Answers (1)

Edward
Edward

Reputation: 19514

I modified your code a little. Thanks to Nic3500 for formatting the code. You needed to add the same labels to the shape manual override as the others and turn the error bar legend off. I also increased the size of the legend because the default seems too small when showing different line types.

ggplot(tgc, 
  aes(
    names, 
    NormalizedJerk, 
    group = interaction(Training, Trial_type), 
    color = interaction(Training, Trial_type), 
    linetype = interaction(Training, Trial_type), 
    shape = Training
  )
) +  
  geom_errorbar(aes(ymin=NormalizedJerk-se, ymax=NormalizedJerk+se),
                show.legend=FALSE, # <- here
                width=.3, position=pd) +
  geom_line(position=pd, size = 1) +
  geom_point(position=pd, size= 2) +
  scale_colour_manual(name = "Experimental group", 
                      values=c("#999999","#999999","#000000","#000000"), 
                      labels=c("RRR","LLL","LRL","RLR")) +
  scale_shape_manual(name = "Experimental group", values=c(19,18,19,18),
                        labels=c("RRR","LLL","LRL","RLR")) +  # <- here
  scale_linetype_manual(name = "Experimental group", 
                        values=c("solid","dashed","solid","dashed"), 
                        labels=c("RRR","LLL","LRL","RLR")) + 
  theme_bw()+  
  facet_wrap(.~condition) + 
  theme(axis.title.y = element_text(vjust= 1.8, size = 14),
        axis.title.x = element_text(vjust= -0.5, size = 14),
        axis.title = element_text(face = "bold"),
        legend.key.width = grid::unit(1.25, "cm")) +  # <- here
  xlab("Block of trials") + ylab("Normalized Jerk") 

enter image description here

Upvotes: 1

Related Questions