ggplot:在折线图中更改geom_text的颜色

时间:2015-12-02 16:42:30

标签: r colors ggplot2 linechart

我在ggplot中有这个折线图: line chart

我使用此代码生成它:

ggplot(data_long, aes(x=time, y=wert, colour=partei)) +
    geom_line() +
    geom_point() +
    labs(y = "Wähleranteil [ % ]", x = NULL, fill = NULL) +
    theme_bw() + theme( strip.background  = element_blank(),
                        panel.grid.major = element_line(colour = "grey80"),
                        panel.border = element_blank(),
                        axis.ticks = element_blank(),
                        panel.grid.minor.x=element_blank(),
                        panel.grid.major.x=element_blank() ) +
    theme(legend.position="bottom") +
    scale_x_date(labels = date_format("%d. %m. %Y")) +
    scale_color_manual(values=farb) +
    geom_text(aes(label = wert, x = time, y = wert+2, ymax = wert+3),
            size = 2, position = position_dodge(width=0.8), hjust=0.25)

正如您所看到的,文本标签也是彩色的,我希望它们是黑色的。如果我在 geom_text 中添加colour="black",则会收到错误消息。如何将文本颜色更改为黑色,同时将线条保留为颜色?

感谢您的帮助。

编辑:

我将color命令添加到geom_text中,如下所示:

geom_text(aes(label = wert, x = time, y = wert+2, ymax = wert+3, colour="black"),
            size = 3, position = position_dodge(width=0.8), hjust=0.25)

错误是: 手动刻度值不足。需要8个,但只提供了7个。

farb的定义如下:

c("#428953", "#CE2929", "#5675D6", "#FF8B07", "#A3DD57", "#77E599", 
"#D0B100")

data_long看起来像这样

structure(list(partei = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L), .Label = c("SVP", "SP", 
"FDP", "CVP", "GPS", "GLP", "BDP"), class = "factor"), kat = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L
), .Label = c("zeit1", "zeit2", "zeit3", "zeit4", "zeit5", "zeit6", 
"zeit7"), class = "factor"), wert = c(26.6, 18.7, 15.1, 12.3, 
8.4, 5.4, 5.4, 24.18, 19.41, 12.97, 10.78, 7.96, 4.98, 5.05, 
26.02, 20.33, 14.21, 12.58, 6.62, 4.14, 5.85, 24.19, 19.76, 14.84, 
10.55, 8.61, 5.24, 4.18, 31.6, 17.41, 16.6, 10.55, 7.22, 5.43, 
4.94, 29.49, 18.03, 17.01, 13.03, 8.06, 5.84, 4.93, 29.4, 18.8, 
16.4, 11.6, 7.1, 4.6, 4.1), time = structure(c(15248, 15248, 
15248, 15248, 15248, 15248, 15248, 15901, 15901, 15901, 15901, 
15901, 15901, 15901, 16117, 16117, 16117, 16117, 16117, 16117, 
16117, 16360, 16360, 16360, 16360, 16360, 16360, 16360, 16481, 
16481, 16481, 16481, 16481, 16481, 16481, 16617, 16617, 16617, 
16617, 16617, 16617, 16617, 16679, 16679, 16679, 16679, 16679, 
16679, 16679), class = "Date")), .Names = c("partei", "kat", 
"wert", "time"), row.names = c(NA, -49L), class = "data.frame")

希望这有点澄清

0 个答案:

没有答案