根据ggplot中的线型和颜色添加图例 [英] Add legend based on line types and colors in ggplot

查看:94
本文介绍了根据ggplot中的线型和颜色添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 plot()函数创建了以下图,我想将其转换为 ggplot()并在线条类型中添加颜色,例如:

以及'predicted'(普通线)和'observed'值(虚线)的图例,例如:

这是我的代码:

 #首先创建一些数据scoregroepen<-seq(从= 1,到= 8,by = 1)s_toets_observed<-c(0.18,0.31,0.42,0.53,0.64,0.75,0.84,0.95)s_toets_predicted<-c(0.20,0.29,0.40,0.55,0.66,0.75,0.85,0.94)s_toets_conf_low<-s_toets_observed-0.03s_toets_conf_high<-s_toets_observed + 0.045图(scoregroepen,s_toets_predicted,type ="b",ylab ="proporties",ylim = c(0,1))行(scoregroepen,s_toets_observed,type ="b",lty = 2)行数(scoregroepen,s_toets_conf_low,lty = 2)行数(scoregroepen,s_toets_conf_high,lty = 2) 

解决方案

尝试接近您期望的方法.我已经在数据框中重新排列了变量,以对它们进行整形,然后绘制出草图.这里的代码:

 库(ggplot2)图书馆(dplyr)图书馆(tidyr)#首先创建一些数据scoregroepen<-seq(从= 1,到= 8,by = 1)s_toets_observed<-c(0.18,0.31,0.42,0.53,0.64,0.75,0.84,0.95)s_toets_predicted<-c(0.20,0.29,0.40,0.55,0.66,0.75,0.85,0.94)s_toets_conf_low<-s_toets_observed-0.03s_toets_conf_high<-s_toets_observed + 0.045df<-data.frame(scoregroepen,s_toets_observed,s_toets_predicted,s_toets_conf_low,s_toets_conf_high)#阴谋df%>%pivot_longer(-scoregroepen)%&%ggplot(aes(x = scoregroepen,y = value,color = name,linetype = name))+geom_line()+geom_point(aes(shape = name))+scale_color_manual(values = c('blue','blue','tomato','cyan3'),breaks = c('s_toets_observed','s_toets_predicted'),labels = c('Observed','Predicted'))+scale_shape_manual(values = c(NA,NA,1,4),breaks = c('s_toets_observed','s_toets_predicted'),labels = c('Observed','Predicted'))+scale_linetype_manual(values = c('dotted','dotted','dashed','solid'),breaks = c('s_toets_observed','s_toets_predicted'),labels = c('Observed','Predicted'))+实验室(color ='var',shape ='var',linetype ='var') 

输出:

I have created the following plot using plot() function and I would like to convert it to ggplot() and add colors in the line types like:

and also a legend for 'predicted' (normal line) and 'observed' values (dashed line) like:

Here is my code:

# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045

plot(scoregroepen,s_toets_predicted, type="b", ylab = "proporties", ylim = c(0,1))
lines(scoregroepen, s_toets_observed, type="b", lty = 2 )
lines(scoregroepen, s_toets_conf_low, lty = 2 )
lines(scoregroepen, s_toets_conf_high, lty = 2 )

解决方案

Try this which is close to what you expect. I have re arranged your variables in a dataframe to reshape them and then sketch the plot. Here the code:

library(ggplot2)
library(dplyr)
library(tidyr)
# Creating some data first
scoregroepen <- seq(from = 1, to = 8, by = 1)
s_toets_observed <- c(0.18, 0.31, 0.42, 0.53, 0.64,0.75,0.84,0.95)
s_toets_predicted <- c(0.20, 0.29, 0.40, 0.55, 0.66, 0.75, 0.85, 0.94)
s_toets_conf_low <- s_toets_observed-0.03
s_toets_conf_high <- s_toets_observed+0.045
df <- data.frame(scoregroepen,s_toets_observed,s_toets_predicted,
                 s_toets_conf_low,s_toets_conf_high)
#Plot
df %>% pivot_longer(-scoregroepen) %>%
  ggplot(aes(x=scoregroepen,y=value,color=name,linetype=name))+
  geom_line()+
  geom_point(aes(shape=name))+
  scale_color_manual(values=c('blue','blue','tomato','cyan3'),
                     breaks=c('s_toets_observed','s_toets_predicted'),
                     labels=c('Observed','Predicted'))+
  scale_shape_manual(values=c(NA,NA,1,4),
                     breaks=c('s_toets_observed','s_toets_predicted'),
                     labels=c('Observed','Predicted'))+
  scale_linetype_manual(values=c('dotted','dotted','dashed','solid'),
                        breaks=c('s_toets_observed','s_toets_predicted'),
                        labels=c('Observed','Predicted'))+
  labs(color='var',shape='var',linetype='var')

Output:

这篇关于根据ggplot中的线型和颜色添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆