1

I have this line chart in ggplot: line chart

I use this code to generate it:

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)

As you can see, the text labels are colored as well, I would like them to be black. If I add colour="black" in geom_text I get an error. How can I change the text color to black while leaving the lines in their colours?

Thanks for any help.

Edit:

I added the colour command to geom_text as follows:

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)

The error is: Insufficient values in manual scale. 8 needed but only 7 provided.

farb is defined like this:

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

data_long looks like this

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")

hope this clarifies it a bit

Mario
  • 2,393
  • 2
  • 17
  • 37
  • what error do you get? where did you try to add `colour="black"` exactly? Also, your example is not reproducible, since we don't have data_long. you should post the output of `dput(data_long)`. See this post: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – scoa Dec 02 '15 at 16:50
  • 10
    Try placing `colour = "black"` outside of the aes function in your geom_text (e.g. `geom_text(aes(...), colour = "black", ...)`) – ialm Dec 02 '15 at 16:52
  • great, this solves it. thanks @ialm ! – Mario Dec 02 '15 at 16:55
  • 4
    More generally, arguments only need to be inside `aes` if they vary with your data. – jeremycg Dec 02 '15 at 17:04

0 Answers0