Reputation: 183
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?
Upvotes: 0
Views: 1731
Reputation:
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))
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)
Upvotes: 1