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

Fernando Velasco Borea

I want to change the legend labels of this plot: enter image description here

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?

gersht

Personally, I would just add a column with the transformed values, i.e. 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))

enter image description here

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

enter image description here

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)

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I change the legend linetype in ggplot2?

How can I edit legend using ggplot2 in R?

How to change scientific notation on legend labels in ggplot2

In ggplot2, how to change the legend labels but leave the colors unchanged?

change the key labels in a legend in ggplot2

change labels in legend for ggplot2

How can I change axis labels from scientific format to power format using ggplot2 and scales?

How can I manually place the NA entry in R ggplot2 discrete map legend?

How to change legend type with ggplot2 in R?

How can I mathematically transform the default labels on legend in ggplot?

How can I replace legend key in ggplot2?

R - Correlation heatmap created with ggplot2: How can I flip the labels on the y-axis?

How can I rotate labels in ggplot2?

Legend Trouble in R- how to change legend text in ggplot2

ggplot2: how to change the order of a legend

How to change items in a ggplot2 legend?

How can I completely remove legend.key from ggplot2 legend?

How can I change the scale of the legend in highcharter in R?

How can I change the position of the legend in this R-code?

Manual legend labels for line graph ggplot2 in R

R ggplot2: changing the legend title and labels in this example dataset

how to format multiple labels into a single legend in ggplot2

How to add Latex code in ggplot2 legend labels?

How to extract the legend labels from a ggplot2 object?

How to set tick labels to edges of continuous ggplot2 legend

How I can change the orient of labels on echarts4r?

How do I change the shape of the legend key glyph to a hexagon in ggplot2?

ggplot2 stacked column: How do I sort by date, use custom colors and change the legend?

How do I change line color in ggplot2 without legend disappearing?