简体   繁体   中英

How can I change the legend labels in R (ggplot2)

I want to change the legend labels of this plot: 在此处输入图片说明

Basically to be converted to the actual value multiplied by 100 (to be able to show them in %). I saved in a vector the values I want to use on the labels already modified and as strings but when I use scale_color_manual I need to specify other things that I'm not sure what they are. Here's my code:

library(tidyverse)

#Get desired amounts

month_income <- seq(500,10000, by = 500)

#Get average monthly % growth

month_perc <- seq(0.03, 0.1, by = 0.01)
perc_vals <- length(month_perc)
perc_title <- as.character(month_perc * 100)

#Preparate data

month_income <- rep(month_income, length(month_perc))

month_perc <- rep(month_perc, length(month_income) / perc_vals) %>% sort()

#Calculate account size and build data frame

dat <- data.frame(Desired_Income = month_income, Monthly_Gain = month_perc, Account_Size = month_income / month_perc)

dat <- dat %>% mutate(Monthly_Gain = as.factor(Monthly_Gain))

#Plot it

dat %>% ggplot(aes(Desired_Income/1000, Account_Size/1000, color = Monthly_Gain)) +
  geom_point() +
  geom_line() +
  xlab("Desired Income in Thousand Dollars") +
  ylab("Required Account Size in Thousand Dollars") + 
  ggtitle("3% to 5% per month account growth") + 
  labs(col = "Monthly Gain") +
  theme(plot.title = element_text(hjust=0.5))

Is there a layer just like ggtitle() that I can use to pass there the vector with the labels?

Personally, I would just add a column with the transformed values, ie instead of:

dat <- dat %>% mutate(Monthly_Gain = as.factor(Monthly_Gain))

I would use:

dat <- dat %>% mutate(`Monthly_Gain_%` = as.factor(Monthly_Gain * 100))

I would then use Monthly_Gain_% as my color variable.

dat %>% ggplot(aes(Desired_Income/1000, Account_Size/1000, color = `Monthly_Gain_%`)) +
    geom_point() +
    geom_line() +
    xlab("Desired Income in Thousand Dollars") +
    ylab("Required Account Size in Thousand Dollars") + 
    ggtitle("3% to 5% per month account growth") + 
    labs(col = "Monthly Gain") +
    theme(plot.title = element_text(hjust=0.5))

在此处输入图片说明

scale_color_manual() will also work, but may require more tinkering with the colors, depending on your needs. For example, to get:

在此处输入图片说明

You would load RColorBrewer and use:

library(RColorBrewer)

dat %>% ggplot(aes(Desired_Income/1000, Account_Size/1000, color = Monthly_Gain)) +
    geom_point() +
    geom_line() +
    xlab("Desired Income in Thousand Dollars") +
    ylab("Required Account Size in Thousand Dollars") + 
    ggtitle("3% to 5% per month account growth") + 
    labs(col = "Monthly Gain") +
    theme(plot.title = element_text(hjust=0.5)) + 
    scale_color_manual(labels = perc_title, values = brewer.pal(8, "Spectral"))

If you simply want to use the default colors as you have above, use scale_color_discrete() instead ( scale_color_hue() would also work):

dat %>% ggplot(aes(Desired_Income/1000, Account_Size/1000, color = Monthly_Gain)) +
    geom_point() +
    geom_line() +
    xlab("Desired Income in Thousand Dollars") +
    ylab("Required Account Size in Thousand Dollars") + 
    ggtitle("3% to 5% per month account growth") + 
    labs(col = "Monthly Gain") +
    theme(plot.title = element_text(hjust=0.5)) + 
    scale_color_discrete(labels = perc_title)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM