【问题标题】:Colouring scatter-plot by group in qplot在 qplot 中按组着色散点图
【发布时间】:2015-05-22 14:14:46
【问题描述】:

我有一个如下所示的数据框 (data):

Condition1   Condition2   Significant?
2.4           5.3            no
9.3           15.9           no
12.1          121.1          yes
0.2           151.3          yes

我正在读取 data.frame 并使用 qplot 绘制如下散点图:

data<-read.table("input.txt",header=TRUE, row.names=1)

con<-data[,1]
con<-data[,2]
sig<-data[,3]

qplot(con, exp, data = data) + 
geom_point(aes(color=sig), size=I(0.6))

我正在尝试:

a) 按重要性为点着色:'yes' = 红色,'no' = 黑色

b) 根据重要性调整点的大小:'yes' = size=I(0.8), 'no' = size=I(0.5)

我可以使用上面的代码生成默认配色方案,但我不知道如何为不同的组自定义颜色和大小 - 我想如果你知道 R 的方式,这很简单,但是我没有!

【问题讨论】:

  • 为什么刚刚发布答案的用户删除了它?我打算接受它!
  • 对不起,我认为它与首先发布的@Jazzurros 不同。唯一的区别实际上是将大小映射到一个因子而不是一个数字。
  • @user20650 - 主要区别在于您的答案并未假定我的示例中的值是真实的(它们显然不是),因此更便携

标签: r ggplot2 scatter-plot


【解决方案1】:

您可以使用scale_size_manualscale_colour_manual 设置颜色和大小,您应该将这两者都添加到aesthetics

dat <- read.table(text="Condition1   Condition2   Significant
2.4           5.3            no
9.3           15.9           no
12.1          121.1          yes
0.2           151.3          yes", header=T)

library(ggplot2)

ggplot(dat, aes(Condition1, Condition2, colour=Significant, size=Significant)) +
  geom_point() +
  scale_size_manual(values=c(0.5, 0.8)*10) +
  scale_colour_manual(values=c('no'="black", 'yes'="red"))

【讨论】:

  • @jazzurro - 我相信你的回答很棒,但作为一个对 R 不是特别熟悉的人,很难适应我的需求。尽管如此 - 感谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-08
  • 1970-01-01
相关资源
最近更新 更多