0

I want to change the title and the labels in a ggplot. I now have the following code:

ggplot() +
geom_line(data = VW_activations, aes(x = VW_activations$Month, y = 
VW_activations$Activations, color = VW_activations$Country), size = 1.2)+
theme_light(base_size = 11, base_family = "")+
xlab("Date")+
ylab("Number of contract activations")+
scale_fill_discrete(name="Country", labels=c("AT", "BE", "CH", "DE", "ES", 
"FI", "FR", "IT", "LU", "NL", "NO", "PT", "UK"))

However, this does not work and the legend title is just VW_activations$Country. Does anyone know what I am doing wrong?

Activation
  • 93
  • 6
  • Welcome to SO! Please, read [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, in `ggplot`, generally it's not nice to use `data$field`, in your case, seems useless `VW_activations$...`. – s__ Oct 09 '18 at 07:13
  • Thanks! Do you maybe also know how to change the label names in the legend? – Activation Oct 09 '18 at 07:37
  • My feeling is legend title is not easy to fine tune in ggplot2. You can either change the column name by `colnames()`, or add a new temp column to your data frame, or see [this answer](https://stackoverflow.com/questions/14622421/how-to-change-legend-title-in-ggplot). And yes, it is not wise to use `$` in `aes()`. – HongboZhu Oct 09 '18 at 07:41

2 Answers2

0

What about this:

ggplot() +
  geom_line(data = VW_activations, aes(x = Month, y = Activations, color = Country), size = 1.2)+
  theme_light(base_size = 11, base_family = "")+
  xlab("Date")+
  ylab("Number of contract activations")+
  scale_color_manual(name="Country", labels=c("AT", "BE"), values =c("red","green"))

To have not manually the number of colors, you can use the package RColorBrewer:

library(RColorBrewer)
ggplot() +
  geom_line(data = VW_activations, aes(x = Month, y = Activations, color = Country), size = 1.2)+
  theme_light(base_size = 11, base_family = "")+
  xlab("Date")+
  ylab("Number of contract activations")+
  scale_color_manual(name="Country", labels=c("AT", "BE"),
  # here we define a scale of color between red and blue f.e., and generate
  # n colors n == numbers of country in this case
  values =colorRampPalette(c("red", "blue"))(length(unique(VW_activations$Country)))
   )

Or also:

... values = brewer.pal(n = length(unique(VW_activations$Country)), name = "Set2")...

To have not ramp "fading" colors. With data:

VW_activations <-data.frame(Country = c("K","J","K","J"),
                            Month = c(1,1,2,2),
                            Activations = c(11,13,55,66))
s__
  • 9,270
  • 3
  • 27
  • 45
0

Does this work? (It is easier to answer your question if you attach some data).

ggplot() +
    geom_line(data = VW_activations, aes(x = VW_activations$Month, 
                                         y = VW_activations$Activations, 
                                         color = VW_activations$Country), 
              size = 1.2) +
    theme_light(base_size = 11, base_family = "") +
    labs(
        x = "Date",
        y = "Number of contract activations",
        fill = "Country"
        ) +
    scale_fill_discrete(labels = c("AT", "BE", "CH", "DE", "ES", "FI", "FR", 
                                   "IT", "LU", "NL", "NO", "PT", "UK"))
ChristianL
  • 140
  • 7
  • Unfortunately not, the labels and legend title stay the same. It is difficult to post my data here since it's consists of thousands of rows – Activation Oct 09 '18 at 08:11