r - 有没有办法在同一个 ggplot 上使用 2 个色标?
问题描述
将数据按类别(样本 A 和 B)分开,制作了 2 层,一层用于点,一层用于线。我想按类别分隔我的数据,指示点的颜色,并分隔线,但颜色与用于点的颜色不同。
library(ggplot2)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
geom_line(aes(color = Sample)) +
scale_color_manual(values = c("red", "blue")) + #for poits
scale_color_manual(values = c("orange", "purple")) #for lines
解决方案
使用ggnewscale
包可以这样实现:
library(ggplot2)
library(ggnewscale)
Sample <- c("a", "b")
Time <- c(0,1,2)
df <- expand.grid(Time=Time, Sample = Sample)
df$Value <- c(1,2,3,2,4,6)
ggplot(data = df,
aes(x = Time,
y = Value)) +
geom_point(aes(color = Sample)) +
scale_color_manual(name = "points", values = c("red", "blue")) + #for poits
new_scale_color() +
geom_line(aes(color = Sample)) +
scale_color_manual(name = "lines", values = c("orange", "purple")) #for lines
推荐阅读
- typescript - 迭代不同类型对象的最佳方法
- python - 追加数据后列表返回空
- fonts - 如何将字体单位(具有一定字体大小)转换为像素?
- sql - 我需要创建一个列,其中相同值保留在表中的次数。SQL 服务器
- flutter - Dart 中的单行构造函数赋值
- python - 已安装 PYSMB 丢失
- javascript - 无法将电话号码显示为 E. 164 的格式。使用 intl-tel-input
- asp.net-mvc - 如何访问在某个控制器中创建的 Viewbag 中的项目并在另一个控制器中使用它?
- python - 这些 while 语句的顺序如何工作?
- node.js - 在开玩笑测试中关闭猫鼬连接会导致匹配器错误