如何更改R(ggplot2)中的图例标签

如何更改R(ggplot2)中的图例标签,r,ggplot2,R,Ggplot2,我要更改此绘图的图例标签: 基本上转换为实际值乘以100(以百分比表示)。我将要在已修改的标签上使用的值作为字符串保存在向量中,但当我使用scale\u color\u manual时,我需要指定其他我不确定的内容。这是我的密码: library(tidyverse) #Get desired amounts month_income <- seq(500,10000, by = 500) #Get average monthly % growth month_perc <

我要更改此绘图的图例标签:


基本上转换为实际值乘以100(以百分比表示)。我将要在已修改的标签上使用的值作为字符串保存在向量中,但当我使用
scale\u color\u manual
时,我需要指定其他我不确定的内容。这是我的密码:

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))
库(tidyverse)
#得到想要的数量

月收入个人而言,我只需添加一列转换后的值,即,而不是:

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

scale\u color\u manual()。例如,要获得:

您将加载
RColorBrewer
并使用:

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"))
如果您只想使用上面的默认颜色,请改用
scale\u color\u discrete()
scale\u color\u hue()
也可以):


缩放颜色手册
是一种正确的方法。只需使用标签选项和值选项<代码>比例\颜色\手册(标签=我的标签,值=c(1:8))
mylabels
是所需标签的向量。这里的值是要使用的颜色的值,这里我只是默认为调色板中的前8种颜色。
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))
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"))
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)