如何更改图例中的标签?

时间:2019-06-06 13:32:56

标签: r ggplot2 graph legend

我想将图例中的标签从1、2、3、4更改为特定标签,例如“稳定低”,“治疗增加”,... 但这根本行不通。有什么想法吗?

我尝试过scale_fill_manual,scale_colour_manual,更改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)

实际结果显示数据框中定义的1、2、3、4。我想要 在我的图例中有实际标签。

original version

second version using scale_colour_manual as proposed in the comments

2 个答案:

答案 0 :(得分:0)

由于要说明的是图的colour,因此可以在scale_colour_manual内完成:

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

答案 1 :(得分:0)

如果在scale_shape_manual内部分配与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')) +

enter image description here