3

I just finished producing a plot in ggplot however there is an "a" character in the legend that I want to remove, I just want to use the corresponding colours to indicate the legend, also I want to edit the legend by changing the legend title.

data10 <- structure(list(GROUP = c("RLR", "LRL", "RLR", "LRL", "RLR", "LRL", 
"RRR", "LLL", "RRR", "LLL", "RRR", "LLL"), conditon2 = structure(1:12, .Label = c("T-No Midline crossing_RLR", 
"T-No Midline crossing_LRL", "T-Midline_RLR", "T-Midline_LRL", 
"T-Midline crossing_RLR", "T-Midline crossing_LRL", "R-No Midline crossing_RRR", 
"R-No Midline crossing_LLL", "R-Midline_RRR", "R-Midline_LLL", 
"R-Midline crossing_RRR", "R-Midline crossing_LLL"), class = "factor"), 
    condition = c("No Midline crossing", "No Midline crossing", 
    "Midline", "Midline", "Midline crossing", "Midline crossing", 
    "No Midline crossing", "No Midline crossing", "Midline", 
    "Midline", "Midline crossing", "Midline crossing"), Trial_type = c("transfer", 
    "transfer", "transfer", "transfer", "transfer", "transfer", 
    "retention", "retention", "retention", "retention", "retention", 
    "retention"), Jam = c(11, 8, 10, 7, 12, 9, 1, 5, 3, 4, 2, 
    6), Training = c("left", "right", "left", "right", "left", 
    "right", "right", "left", "right", "left", "right", "left"
    ), N = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8), MovementTime_102 = c(1690.390625, 
    2266.875, 1649.21875, 1623.59375, 3128.660714, 2608.4375, 
    1701.40625, 2200.234375, 2018.75, 1940.625, 2505.703125, 
    1950.078125), MovementTime_104 = c(1616.71875, 2013.359375, 
    1390.859375, 1435.15625, 2399.765625, 2302.109375, 1336.40625, 
    1658.046875, 1410.625, 1601.875, 1684.53125, 1573.839286), 
    Pathlength_102 = c(2.823023969, 3.450548063, 3.057751688, 
    3.184808844, 3.831920395, 2.779296859, 2.790851078, 3.872267719, 
    2.991207031, 2.8680385, 3.276254563, 2.808966672), Pathlength_104 = c(2.628399641, 
    3.408170609, 2.655326156, 3.017285625, 2.975724094, 2.625024781, 
    2.661606391, 2.945664516, 2.636576609, 2.65516175, 2.782565766, 
    2.568061634), NormalizedJerk_102 = c(1853.413926, 3900.411917, 
    1411.201689, 1391.973644, 6830.819063, 4015.516784, 1340.465366, 
    2981.812369, 2310.589311, 2060.157118, 5617.967587, 2089.925187
    ), NormalizedJerk_104 = c(1394.847247, 2335.718672, 847.952528, 
    914.3836443, 3915.019566, 2563.167235, 765.2752941, 1508.285476, 
    1023.170254, 1092.701687, 1705.629422, 1269.670456), AveResultantVel_102 = c(2.162818172, 
    2.037483406, 2.374258766, 2.449869109, 1.467132962, 1.198650234, 
    1.983419094, 1.994460031, 2.101220406, 2.021215719, 1.595565484, 
    1.789839578), AveResultantVel_104 = c(2.113288797, 2.213037, 
    2.39646225, 2.822947859, 1.481807047, 1.311399453, 2.54589975, 
    2.193130625, 2.644351188, 2.317185313, 2.014750766, 2.113643324
    ), EndpointError_102 = c(2.03515938, 1.83768956, 1.374642, 
    1.57970041, 2.58900025, 2.14219202, 1.64171472, 2.05362611, 
    2.28107478, 1.62285542, 2.13489883, 2.27036917), EndpointError_104 = c(1.593606016, 
    1.404618047, 1.01018125, 1.239002656, 1.672676594, 1.796644641, 
    1.503123406, 1.477764016, 1.483629813, 0.979220453, 1.370374047, 
    1.621229031), MT_diff = c(-73.67, -253.52, -258.36, -188.44, 
    -728.9, -306.33, -365, -542.19, -608.13, -338.75, -821.17, 
    -376.24), PL_diff = c(-0.19, -0.04, -0.4, -0.17, -0.86, -0.15, 
    -0.13, -0.93, -0.35, -0.21, -0.49, -0.24), NJ_diff = c(-458.57, 
    -1564.69, -563.25, -477.59, -2915.8, -1452.35, -575.19, -1473.53, 
    -1287.42, -967.46, -3912.34, -820.25), RV_diff = c(-0.05, 
    0.18, 0.02, 0.37, 0.01, 0.11, 0.56, 0.2, 0.54, 0.3, 0.42, 
    0.32), EE_diff = c(-0.44, -0.43, -0.36, -0.34, -0.92, -0.35, 
    -0.14, -0.58, -0.8, -0.64, -0.76, -0.65), country = structure(c(5L, 
    8L, 6L, 9L, 4L, 7L, 2L, 11L, 3L, 12L, 1L, 10L), .Label = c("R-Midline crossing_RRR", 
    "R-No Midline crossing_RRR", "R-Midline_RRR", "T-Midline crossing_RLR", 
    "T-No Midline crossing_RLR", "T-Midline_RLR", "T-Midline crossing_LRL", 
    "T-No Midline crossing_LRL", "T-Midline_LRL", "R-Midline crossing_LLL", 
    "R-No Midline crossing_LLL", "R-Midline_LLL"), class = "factor")), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))
library(tidyverse)
library(ggalt)


data10$conditon2 <- reorder(data10$conditon2, data10$Trial_type)

data10$conditon2 <- factor(data10$conditon2, levels = levels(data10$conditon2))

ggplot(data = data10,
       aes(y=conditon2, x=MovementTime_102, xend=MovementTime_104, 
           colour = Trial_type)) +
  labs(x=NULL, y=NULL, title="Change in Movement time (ms)") +
  geom_dumbbell() +

  scale_color_manual(values = c('retention' = ' blue', 'transfer' = 'red'))+

  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
            aes(x=MovementTime_104, y=conditon2, label="Pre-test"),
            color="black", size=3, vjust=-3,hjust=0.75) +
  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"),
            aes(x=MovementTime_102, y=conditon2, label="Post-test"),
            color="grey40", size=3, vjust=-3,hjust=-0.1) +
  geom_rect(data=data10, aes(xmin=3500, xmax=4000, ymin=-Inf, ymax=Inf), fill="grey") +
  geom_text(data=data10, aes(label=paste0(MT_diff), y=conditon2, x=3750), fontface="bold",size=3) +
  geom_text(data=filter(data10, conditon2=="R-Midline crossing_LLL"), 
            aes(x=3750, y=conditon2, label="Difference"),
            color="black", size=3.1, vjust=-2, fontface="bold") +
  scale_x_continuous(expand=c(0,0), limits=c(1000, 4100)) +
  scale_y_discrete(expand=c(0.1,0))  + 
  theme_bw()  

Sample of legend

Edward
  • 10,360
  • 2
  • 11
  • 26
  • Please visit [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – UseR10085 Jul 18 '20 at 07:39
  • @Reuben Newton Addison Did you forget your question and the answer to this. Please have a look also to the comments there. https://stackoverflow.com/questions/62948392/differentiate-lines-in-geom-dumbbel-by-colour-using-levels-of-a-variable/62948851#62948851 – MarBlo Jul 18 '20 at 11:00

2 Answers2

2

You can hide the legend in the geom_text function:

geom_text(data=data10, aes(label=paste0(MT_diff), y=conditon2, x=3750), 
          fontface="bold", size=3, show.legend=FALSE) +

and possibly even in the geom_rect function:

  geom_rect(data=data10, aes(xmin=3500, xmax=4000, ymin=-Inf, ymax=Inf), 
            fill="grey", show.legend = FALSE) +

For the legend title, add:

guides(col=guide_legend(title="New title here"))

enter image description here

Edward
  • 10,360
  • 2
  • 11
  • 26
  • For the legend titles I like better using `+ labs(x = "X axis", y = "y axis", colour = "color legend title", title = "plot title")`. It is easier to remember than the guide_legend approach, at least for me. – zeehio Jul 18 '20 at 11:05
0

The answer is in the comment of your original question - together with the solution to your base question: Differentiate lines in geom_dumbbel by colour using levels of a variable

Take the aes for color out of the ggplot and put it into the dumbell like so: geom_dumbbell(aes(colour = factor(Trial_type))).

MarBlo
  • 4,195
  • 1
  • 13
  • 27