ggplot中的geom_tile覆盖图与点一起绘制

3
severity <- c("Major","Serious","Minor","Negligible")
probability <- c("Highly Probable","Probable","Possible","Remote","Unlikely","Impossible")
df <- expand.grid(x=severity,y=probability)
df$x <- factor(df$x, levels=rev(unique(df$x)))
df$y <- factor(df$y, levels=rev(unique(df$y)))
df$color <- c(1,1,2,2,1,2,2,2,2,2,2,3,2,2,3,3,2,3,3,3,3,3,3,3)

ggplot(df,aes(x,y,fill=factor(color)))+
geom_tile(color="black")+
scale_fill_manual(guide="none",values=c("red","yellow","green"))+
scale_x_discrete(expand=c(0,0))+scale_y_discrete(expand=c(0,0))+
labs(x="",y="")

生成风险评估得分卡图表。我想通过添加记录使用CSV文件来增加点数。每个记录有3个字段,即项名称、x坐标和y坐标。其中,x表示严重程度,y表示概率。

   da <- data.frame(list(name=c("ENVIRONMENTAL","COSTS","SUPPLY","HEALTH"),
                    severity=c("Major","Serious","Minor","Serious"),
                    probability=c("Probable","Possible","Probable","Unlikely")))
da
           name severity probability
1 ENVIRONMENTAL    Major    Probable
2         COSTS  Serious    Possible
3        SUPPLY    Minor    Probable
4        HEALTH  Serious    Unlikely

> p1 <- p + data.frame(da, aes(severity, probability)) + geom_point()
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : 
  cannot coerce class ""uneval"" to a data.frame
> 

> d <- data.frame(list(name=c("ENVIRONMENTAL","COSTS","SUPPLY","HEALTH"),
    severity=c(2,3,4,1),probability=c(3,5,4,6)))
> d
           name severity probability
1 ENVIRONMENTAL        2           3
2         COSTS        3           5
3        SUPPLY        4           4
4        HEALTH        1           6
> ggplot(d,x=severity, y=probability)+ geom_point()
Error in exists(name, envir = env, mode = mode) : 
  argument "env" is missing, with no default

我该如何向ggplot / geom_tile图形添加点呢?

1
除非我错了,data.frame(list(...)) 等同于 data.frame(...) - flies
1个回答

3

你不能直接将data.frame添加到绘图中(至少不是那样添加的)。你可以添加一个新层geom_point(),并指定它从哪个data.frame中获取数据。为了使事情顺利进行,你应该保证仍想使用的美学列(这里是xy)在两个data.frames中具有相同的名称。

# It's better practice to modify your data
# then to convert to factor within the plot
df$color <- factor(c(1,1,2,2,1,2,2,2,2,2,2,3,2,2,3,3,2,3,3,3,3,3,3,3))

# get some meaningful names, that match da and d
names(df)[1:2] <- c("severity", "probability")


p <- ggplot(df, aes(x = severity, y = probability)) +
    # moved fill to the geom_tile layer, because it's only used there
    geom_tile(color = "black", aes(fill = color)) + 
    scale_fill_manual(guide = "none", values = c("red", "yellow", "green")) +
    scale_x_discrete(expand = c(0, 0)) +
    scale_y_discrete(expand = c(0, 0)) +
    labs(x = "", y = "")
    # alsonoticehowaddingspacesmakesiteasiertoread

# Using the same column names? Yup! Now it's this easy:
p + geom_point(data = da) +
    geom_point(data = d, color = "dodgerblue4")

由于在注释中不推荐使用"谢谢"这个词,这是最终的图表。 - mommicked
p <- ggplot(df, aes(x = severity, y = probability)) +
  • # 将 fill 移至 geom_tile 层,因为只在那里使用
  • geom_tile(color = "black", aes(fill = color)) +
  • scale_fill_manual(guide = "none", values = c("red", "yellow", "green")) +
  • scale_x_discrete(expand = c(0, 0)) +
  • scale_y_discrete(expand = c(0, 0)) +
  • labs(x = "严重性", y = "概率") p2 <- p1+ geom_text(data=d, mapping=aes(severity, probability, label=name), size=5, vjust= 2) # 我喜欢
- mommicked

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接