R ggplot中的手动色标和因子
嗨,我正在ggplot中生成以下图表:R ggplot中的手动色标和因子,r,ggplot2,R,Ggplot2,嗨,我正在ggplot中生成以下图表: p<-df_tra %>% filter(Theta_param ==1 & Gamma_param==0.76,Int_dis=='Bench', Rho_param %in% c(0, 1)) %>% ggplot(aes(x = Debt, y = Gini_tra , colour =Rho_param)) + geom_line()+ scale_color_hue(labels = unname(c('
p<-df_tra %>%
filter(Theta_param ==1 & Gamma_param==0.76,Int_dis=='Bench', Rho_param %in% c(0, 1)) %>%
ggplot(aes(x = Debt, y = Gini_tra , colour =Rho_param)) +
geom_line()+
scale_color_hue(labels = unname(c('CD production', 'Linear Technology')))+
#scale_color_manual("",values = rev(RColorBrewer::brewer.pal(3,'Blues')))+
xlab("Public Debt") +
ylab("Wealth Inequality [Gini Index]") +
theme_minimal()+
theme(text =element_text(family="Times New Roman"),
legend.title = element_blank(),
legend.text.align = 0,
axis.title.y = element_text(size = 8),
axis.title.x = element_text(size = 8),
legend.position= c(0.2, 0.9))
print(p)
p%
过滤器(θ参数==1和伽马参数==0.76,Int参数=='Bench',ρ参数%c(0,1))%>%
ggplot(aes(x=债务,y=基尼系数,颜色=ρ参数))+
geom_线()+
比例、颜色、色调(标签=未命名(c(“CD制作”、“线性技术”))+
#scale\u color\u手册(“,value=rev(RColorBrewer::brewer.pal(3,'Blues'))+
xlab(“公共债务”)+
ylab(“财富不平等[基尼指数]”)+
主题_极小值()+
主题(text=element_text(family=“Times New Roman”),
legend.title=元素_blank(),
legend.text.align=0,
轴.title.y=元素\文本(大小=8),
axis.title.x=元素\文本(大小=8),
图例位置=c(0.2,0.9))
印刷品(p)
它给出了以下图片:
现在我想更改颜色,并使用以下代码:
p<-df_tra %>%
filter(Theta_param ==1 & Gamma_param==0.76,Int_dis=='Bench', Rho_param %in% c(0, 1)) %>%
ggplot(aes(x = Debt, y = Gini_tra , colour =Rho_param)) +
geom_line()+
scale_color_hue(labels = unname(c('CD production', 'Linear Technology')))+
**scale_color_manual("",values = rev(RColorBrewer::brewer.pal(3,'Blues')))+**
xlab("Public Debt") +
ylab("Wealth Inequality [Gini Index]") +
theme_minimal()+
theme(text =element_text(family="Times New Roman"),
legend.title = element_blank(),
legend.text.align = 0,
axis.title.y = element_text(size = 8),
axis.title.x = element_text(size = 8),
legend.position= c(0.2, 0.9))
print(p)
p%
过滤器(θ参数==1和伽马参数==0.76,Int参数=='Bench',ρ参数%c(0,1))%>%
ggplot(aes(x=债务,y=基尼系数,颜色=ρ参数))+
geom_线()+
比例、颜色、色调(标签=未命名(c(“CD制作”、“线性技术”))+
**scale\u color\u手册(“,value=rev(RColorBrewer::brewer.pal(3,'Blues'))+**
xlab(“公共债务”)+
ylab(“财富不平等[基尼指数]”)+
主题_极小值()+
主题(text=element_text(family=“Times New Roman”),
legend.title=元素_blank(),
legend.text.align=0,
轴.title.y=元素\文本(大小=8),
axis.title.x=元素\文本(大小=8),
图例位置=c(0.2,0.9))
印刷品(p)
它产生了所需的颜色,但不幸的是,我想要保留的图例已经消失了,取而代之的是我用来复制图形的因子的数值等价物
出了什么问题,第二段代码消除了我的传奇?有人能帮我用第二张图的颜色重现第一张图中的内容吗
我已经附加了使用“dput”的文本文件与我的df复制的数字
运行第二个代码块时,您将看到以下警告: “颜色”的比例已经存在。为“颜色”添加另一个比例,以取代现有比例 它告诉您没有标签的
scale\u color\u manual
正在覆盖scale\u color\u hue
因此,一种解决方案是删除scale\u color\u色调
,并将标签添加到scale\u color\u手册
df_tra %>%
filter(Theta_param ==1 & Gamma_param==0.76,Int_dis=='Bench', Rho_param %in% c(0, 1)) %>%
ggplot(aes(x = Debt, y = Gini_tra, colour = Rho_param)) +
geom_line() +
scale_color_manual(labels = unname(c('CD production', 'Linear Technology')),
values = rev(RColorBrewer::brewer.pal(3,'Blues'))) +
xlab("Public Debt") +
ylab("Wealth Inequality [Gini Index]") +
theme_minimal() +
theme(text = element_text(family = "Times New Roman"),
legend.title = element_blank(),
legend.text.align = 0,
axis.title.y = element_text(size = 8),
axis.title.x = element_text(size = 8),
legend.position = c(0.2, 0.9))
另一种解决方案是使用dplyr::mutate
并用标签替换原始数据中的0/1,因此您不需要手动添加它们