【问题标题】:Effective way of plotting additional points to an exisiting plot在现有地块上绘制附加点的有效方法
【发布时间】:2018-02-05 01:13:59
【问题描述】:

在我的例子中,有 100 个唯一的(XY)点,每个点都有一个 ID,并且属于一个 Type。在这 100 个点中,20 个点具有其他三种类型的值(CTDOP)。

数据生成过程如下:

df <- data.frame(X=rnorm(100,0,1), Y=rnorm(100,0,1), 
                 ID=paste(rep("ID", 100), 1:100, sep="_"),
                 Type=rep("ID",100),
                 Val=c(rep(c('Type1','Type2'),30),
                       rep(c('Type3','Type4'),20)))

随机选择的 20 个点 (sample(1:100,20)) 将具有为这些点添加额外信息的值。这个额外的Type 中的所有这 20 个点将在Type=="ID" 中包含信息。

dat1 <- data.frame(Type=rep('CT',20),
                   Val=paste(rep("CT", 20), 
                             sample(1:6,20,replace=T), sep="_"))
dat1 <- cbind(df[sample(1:100,20),1:3],dat1)

dat2 <- data.frame(Type=rep('D',20),
                   Val=paste(rep("D", 20), 
                             sample(1:6,20,replace=T), sep="_"))
dat2 <- cbind(df[sample(1:100,20),1:3],dat2)

dat3 <- data.frame(Type=rep('OP',20),
                   Val=paste(rep("OP", 20), 
                             sample(1:6,20,replace=T), sep="_"))
dat3 <- cbind(df[sample(1:100,20),1:3],dat3)

df <- rbind(df, dat1, dat2, dat3)

现在,为Type=="D" 绘制具有D_1D_4 值的点。

df %>% filter(Val %in% c('D_1','D_4')) %>% 
  ggplot(aes(X,Y,col=Val)) + geom_point() + geom_text(aes(label=ID))

注意:我添加了 ID geom_text(aes(label=ID)) 仅用于说明目的。

为此,现有的情节,我必须添加剩余的 92 个点,这些点没有超过两个值或根本没有值。我尝试在 Hadley here 提到的现有方法中添加额外的点:

p <- df %>% filter(Val %in% c('D_1','D_4')) %>% ggplot(aes(X,Y,col=Val)) + geom_point() 

p + geom_point(data=df[(!df$ID %in% df$ID[df$Val %in% c('D_1','D_4')]) & df$Type=="ID",],
               colour="grey")

问题:

  1. 如何在单个命令中或以优雅的方式绘制选定点和附加点?

  2. 是否有任何可能的dplyr 方法可以在上述命令中使用?

更新df$Type=="ID" 非常重要,因为它只允许绘制剩余点一次。否则,其中一些点的值在CTDOP 中会导致重复绘图。

df %>% count(X,Y) %>% arrange(desc(n))
# # A tibble: 100 x 3
#             X          Y     n
#         <dbl>      <dbl> <int>
# 1 -0.86266147  2.0368626     4
# 2 -0.61770678  0.4428537     4
# 3  1.32441957 -0.9388095     4
# 4 -1.65650319 -0.1551399     3
# 5 -0.99946809  1.1791395     3
# 6 -0.52881072  0.1742483     3
# 7 -0.25892382  0.1380577     3
# 8 -0.19239410  0.5269329     3
# 9 -0.09709764 -0.4855484     3
# 10 -0.05977874  0.1771422     3
# # ... with 90 more rows

看起来,具有相同 X、Y 值的前三个点的值分别为 Type ID、CT、D、OP。但是这些点只需要绘制一次。

【问题讨论】:

  • 命名数据参数,不要依赖那里的位置匹配。
  • @Roland 谢谢,相应地编辑了问题。
  • 也许我在这里遗漏了一些东西,但你不能在levels 中创建一个只有所需值的因子。 levels 中未指定的值将变为NA,默认情况下它们又被绘制为“灰色”。 ggplot(mtcars, aes(x = hp, y = mpg, col = factor(carb, levels = 1:2))) + geom_point()
  • 简而言之,有 100 个唯一点,但数据框 df 有 160 行。剩下的 60 行是额外三种类型的值。
  • 有什么方法可以限制多次绘制点?有些点会一次又一次地绘制,这可能会影响彩色点

标签: r ggplot2 dplyr


【解决方案1】:

更新答案

解决第一条评论:由于数据中有多行具有相同的XY 坐标,因此多次绘制了一些点。您可以使用下面的代码删除重复点。我们首先根据Val 的顺序对点进行排序,这样重复项将来自Other 点,而不是来自D_1D_4 点(尽管如果您的真实数据包含D_1D_4 点具有相同的XY 坐标,只会绘制D_1 点。

ggplot(df %>% 
         mutate(Val=fct_other(Val,keep=c("D_1","D_4"))) %>% 
         arrange(Val) %>% 
         filter(!duplicated(.[,c("X","Y")])), 
       aes(X,Y,col=Val, size=Val)) + 
  geom_point() +
  scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
  scale_size_manual(values=c(D_1=3, D_4=3, Other=1)) +
  theme_bw() 

如果您想绘制所有D_1D_4 点,即使它们具有相同的XY 坐标,您也可以这样做:

df %>% 
   mutate(Val=fct_other(Val,keep=c("D_1","D_4"))) %>% 
   arrange(X, Y, Val) %>% 
   filter((c(1,diff(X)) != 0 & c(1, diff(Y)) !=0) | Val != 'Other')

然后您可以使用不同的点标记大小来确保重叠绘制的D_1D_4 点都可见。

原答案

像这样折叠Val 的所有其他级别怎么样:

library(tidyverse)
library(forcats)

ggplot(df %>% mutate(Val=fct_other(Val,keep=c("D_1","D_4"))), aes(X,Y,col=Val)) + 
  geom_point() +
  scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
  theme_bw()

您还可以使用大小使所需的点更加突出。对于这个特定的数据集,这种方法还确保我们可以看到隐藏在上图中灰点后面的几个 D_1D_4 点。

ggplot(df %>% mutate(Val=fct_other(Val,keep=c("D_1","D_4"))), aes(X,Y,col=Val, size=Val)) + 
  geom_point() +
  scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65),Other="grey70")) +
  scale_size_manual(values=c(D_1=3, D_4=3, Other=1)) +
  theme_bw()

【讨论】:

  • 很好的建议。我有几个 cmets(不确定我是否正确)。一些点,大约 52 个点被绘制不止一次(即两次或三次)。可以在第二个图中的红点中观察到,该点再次绘制在更大尺寸的红点顶部。你看到了吗?您是否会看到其他方法,其中所有 100 个点仅绘制一次,无论是否着色?
  • 您有兴趣回复question吗?
【解决方案2】:

在 eipi10 的帖子上建立一点

library(tidyverse)
library(forcats)
theme_set(theme_bw(base_size=12)+ 
        theme(panel.grid.major = element_blank(),
              panel.grid.minor = element_blank()))

df <- data_frame(X=rnorm(100,0,1), Y=rnorm(100,0,1), 
             ID=paste(rep("ID", 100), 1:100, sep="_"),
             Type=rep("ID",100),
             Val=c(rep(c('Type1','Type2'),30),
                   rep(c('Type3','Type4'),20)))

f.df <- function(x, type){
          type = deparse(substitute(type))
          df[sample(1:100,20), 1:3] %>% 
          mutate(Type=rep(type, 20),
                 Val=paste(rep(type, 20),
                     sample(1:6,20, replace=T), sep="_"))
}

dat1 <- f.df(df, CT)
dat2 <- f.df(df, D)
dat3 <- f.df(df, OP)

df2 <- bind_rows(df, dat1, dat2, dat3)

df2 %>% 
   mutate(Group = fct_other(Val,keep=c("D_1","D_4"))) %>% 
   ggplot(aes(X,Y,color=Group)) + geom_point() +
   scale_colour_manual(values=c(D_1=hcl(15,100,65),D_4=hcl(195,100,65), 
                       Other="grey70")) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多