简体   繁体   中英

How can I change the labels in the legend?

I would like to change the labels in the legend from 1, 2, 3, 4 to specific labels like "stable low", "treatment increasing", ... But it just won`t work. Any ideas?

I tried scale_fill_manual, scale_colour_manual, changing the aes ...

# Create dataframe
timepoint=c("baseline", "baseline", "baseline", "baseline", 
        "nach briefing", "nach briefing", "nach briefing", "nach 
briefing",
        "session 4", "session 4", "session 4", "session 4")
expectation=c(9.661290, 14.57692, 12.41667, 14.35714, 10.20968, 14.30769, 
17.16667, 15.57143,  9.532258, 15.78846, 15.75000, 10.000000)
ci=c(0.436101, 0.79578, 1.41966, 1.23227, 0.51849, 0.91015, 0.97055, 
1.1511, 
0.467372, 0.70027, 1.58264, 1.260921)
cluster=c(1,2,3,4,1,2,3,4,1,2,3,4)

df.graph <- data.frame(expectation, ci, timepoint, cluster)
df.graph$cluster <- factor(df.graph$cluster) # damit im Graph nach Farbe 
getrennt werden kann


cluster <- ggplot(data=df.graph, aes(x=timepoint, y=expectation, 
group=cluster, colour=cluster)) +
          geom_line(size=1.2, position=position_dodge(0.06)) +
          geom_point(size=4.2, position=position_dodge(0.06), 
aes(shape=cluster)) +
          scale_shape_manual(values=c(15, 16, 17, 18)) + # change shape 
of point manually
          scale_size_manual(values=c(2,3,4, 10)) +
          geom_errorbar(aes(ymin=expectation-ci, ymax=expectation+ci), 
width=.3, linetype="solid", position=position_dodge(0.06)) + # pd damit 
CI- 
Balken etwas verschoben sind
          ylab("Treatment expectations (ETS)") +
          xlab("") +
          scale_y_continuous(limits = c(5, 20), breaks=seq(1, 20, 1)) +
          scale_x_discrete(labels=c("baseline", "after briefing", "after 
session 4")) +
          theme_classic(base_size = 26) +
          scale_colour_manual(values=c("#003f5c", "#bc5090", "#ff6361", 
"#ffa600")) +
          theme(legend.position=c("top"),
                legend.text = element_text(size=20, colour = "gray29", 
family ="Calibri"), 
                legend.key.size = unit(1.7, 'lines'),
                legend.title=element_blank(),
                panel.grid.major = element_blank(), # removes gridlines
                panel.grid.minor = element_blank(), 
                axis.line = element_line(colour = "black"),
                axis.title.y = element_text(colour = "gray29", family 
="Calibri", size=22),
                axis.text.x = element_text(colour = "gray29", size=18, 
family ="Calibri"), # Achesenbeschriftung x-Achse
                axis.text.y = element_text(colour = "gray29", size=18, 
family ="Calibri"),
                panel.background = element_rect(fill = "transparent", 
color = NA), # bg of the panel
                plot.background = element_rect(fill = "transparent", 
color = NA),
                legend.background = element_rect(fill = "transparent", 
size = 0),
                legend.key = element_rect(fill = 'transparent', color = 
NA)) # get rid of legend bg)

The actual result show 1, 2, 3, 4 as defined in the dataframe. I want to have actual labels in my legend.

original version

second version using scale_colour_manual as proposed in the comments

Since what you want to legend is the colour of your plot, you can do it inside scale_colour_manual :

scale_colour_manual(
  values=c("#003f5c", "#bc5090", "#ff6361", "#ffa600"),
  labels = c('whatever', 'you', 'want', 'as label')
  )

It should work if you assign the same labels inside scale_shape_manual as in scale_color_manual.

scale_shape_manual(values=c(15, 16, 17, 18),
                   labels = c('whatever', 'you', 'want', 'as label')) + 
scale_colour_manual(values=c("#003f5c", "#bc5090", "#ff6361", "#ffa600"),
                    labels = c('whatever', 'you', 'want', 'as label')) +

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM