首页 > 解决方案 > 如何更改图例中的标签?

问题描述

我想将图例中的标签从 1、2、3、4 更改为特定标签,如“稳定低”、“治疗增加”……但它不起作用。有任何想法吗?

我试过了scale_fill_manualscale_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。我想在我的传说中有实际的标签。

原版

使用评论中建议的 scale_colour_manual 的第二个版本

标签: rggplot2graphlegend

解决方案


如果您在 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')) +

在此处输入图像描述


推荐阅读