将图例添加到ggplot中不同的颜色和线型

如何解决将图例添加到ggplot中不同的颜色和线型

我已经整天试图获得具有不同线条颜色和类型的线条图,但是现在,我设法做到了,图例不再显示。

数据帧的前10行(共100行):

    Duration   Clouds         1        2        3         4         5         6          7         8
1   15.00000 51.56604  3.319234 7.479382 6.185313  5.620474 30.149642  5.075788   6.145367  4.779686
2   15.30303 51.56604  3.389524 7.437142 6.146534  5.730071 29.224362  5.127665   6.321472  4.831718
3   15.60606 51.56604  3.461303 7.395140 6.107999  5.841805 28.327479  5.180072   6.502624  4.884316
4   15.90909 51.56604  3.534602 7.353375 6.069705  5.955718 27.458121  5.233015   6.688967  4.937487
5   16.21212 51.56604  3.609453 7.311846 6.031652  6.071852 26.615443  5.286498   6.880650  4.991237
6   16.51515 51.56604  3.685889 7.270552 5.993836  6.190251 25.798626  5.340529   7.077825  5.045572
7   16.81818 51.56604  3.763944 7.229491 5.956258  6.310959 25.006878  5.395111   7.280651  5.100498
8   17.12121 51.56604  3.843652 7.188662 5.918916  6.434020 24.239427  5.450252   7.489290  5.156022
9   17.42424 51.56604  3.925048 7.148063 5.881807  6.559481 23.495530  5.505956   7.703907  5.212151
10  17.72727 51.56604  4.008168 7.107694 5.844932  6.687388 22.774462  5.562229   7.924675  5.268891

我的剧情脚本:

> plot.interact1.2 <- ggplot(pred_df2,aes(x=Duration)) + 
+   geom_line(aes(y = `1`),color="#D8C033") + 
+   geom_line(aes(y = `2`),color="#B9A2A3") +
+   geom_line(aes(y = `3`),color="#225F6D") +
+   geom_line(aes(y = `4`),color="#B0946F") +
+   geom_line(aes(y = `5`),color="#D8C033",linetype="dashed") +
+   geom_line(aes(y = `6`),color="#B9A2A3",linetype="dashed") +
+   geom_line(aes(y = `7`),color="#225F6D",linetype="dashed") +
+   geom_line(aes(y = `8`),color="#B0946F",linetype="dashed") +
+   scale_color_discrete(name = "Lunar phase",labels = c("New Moon","Full Moon")) +
+   theme_ipsum() +
+   ylab("Predicted mean values") +
+   xlab("Survey duration")
> plot.interact1.2

Interaction plot

我从关于stackoverflow的另一篇文章中获得了scale_color_discrete()作为类似问题的解决方案,但这似乎对我的情况没有帮助。

解决方法

尝试这种方法,并遵循 @markus @RichardTelford 的建议。具有图例的关键是将数据重塑为长整型。可以使用pivot_longer()完成。然后将必要的元素添加到绘图中。这里的代码:

library(tidyverse)
#Code
df %>% pivot_longer(-c(Duration,Clouds)) %>%
  mutate(name=gsub('X','',name)) %>%
  ggplot(aes(x=Duration,y=value,color=name,group=name,linetype=name))+
  geom_line()+
  scale_color_manual(values=c("#D8C033","#B9A2A3","#225F6D","#B0946F","#D8C033","#B0946F"))+
  scale_linetype_manual(values=c(rep('solid',4),rep('dashed',4)))+
  theme_bw() +
  ylab("Predicted mean values") +
  xlab("Survey duration")

输出:

enter image description here

使用了一些数据:

#Data
df <- structure(list(Duration = c(15,15.30303,15.60606,15.90909,16.21212,16.51515,16.81818,17.12121,17.42424,17.72727),Clouds = c(51.56604,51.56604,51.56604),X1 = c(3.319234,3.389524,3.461303,3.534602,3.609453,3.685889,3.763944,3.843652,3.925048,4.008168),X2 = c(7.479382,7.437142,7.39514,7.353375,7.311846,7.270552,7.229491,7.188662,7.148063,7.107694),X3 = c(6.185313,6.146534,6.107999,6.069705,6.031652,5.993836,5.956258,5.918916,5.881807,5.844932),X4 = c(5.620474,5.730071,5.841805,5.955718,6.071852,6.190251,6.310959,6.43402,6.559481,6.687388
    ),X5 = c(30.149642,29.224362,28.327479,27.458121,26.615443,25.798626,25.006878,24.239427,23.49553,22.774462),X6 = c(5.075788,5.127665,5.180072,5.233015,5.286498,5.340529,5.395111,5.450252,5.505956,5.562229),X7 = c(6.145367,6.321472,6.502624,6.688967,6.88065,7.077825,7.280651,7.48929,7.703907,7.924675),X8 = c(4.779686,4.831718,4.884316,4.937487,4.991237,5.045572,5.100498,5.156022,5.212151,5.268891)),class = "data.frame",row.names = c("1","2","3","4","5","6","7","8","9","10"))

如果您想更改图例中的标签,可以将labels添加到scale_*_*()选项中,如下所示:

#Code 2
df %>% pivot_longer(-c(Duration,"#B0946F"),labels=c('1'='Var1','2'='Var2','3'='Var3','4'='Var4','5'='Var5','6'='Var6','7'='Var7','8'='Var8'))+
  scale_linetype_manual(values=c(rep('solid',4)),'8'='Var8'))+
  theme_bw() +
  ylab("Predicted mean values") +
  xlab("Survey duration")

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-