conditionally set both fill and color in geom_point ggplot R

huangapple go评论65阅读模式
英文:

conditionally set both fill and color in geom_point ggplot R

问题

我的代码有什么问题,导致填充颜色没有改变?

cars %>%
  ggplot() + 
  geom_point(aes(x = speed, y = dist,
                 color= I(ifelse(dist > 50, 'red', 'black')),
                 fill= I(ifelse(dist > 50, 'pink', 'gray'))
                 )
             )

您的代码看起来正确。如果填充颜色没有改变,可能是数据或其他因素导致的。请确保数据中的dist值是否适当,并且包含适当的范围。

英文:

what is wrong my code that does not change the filling color?

cars %>%
  ggplot() + 
  geom_point(aes(x = speed, y = dist,
                 color= I(ifelse(dist >50, 'red', 'black')),
                 fill= I(ifelse(dist >50, 'pink', 'gray'))
                 
                 )
             
             )

conditionally set both fill and color in geom_point ggplot R

答案1

得分: 2

需要有一个允许同时设置填充和颜色的点形状。

library(ggplot2)
cars %>%
    ggplot() + 
    geom_point(
        aes(x = speed, y = dist,
            color = I(ifelse(dist > 50, 'red', 'black')),
            fill = I(ifelse(dist > 50, 'pink', 'gray')),
        ), 
        shape = 21, 
        size = 4 # 更改大小以便于可视化
    )

要查看允许同时设置填充和颜色的点形状,请使用 help(points) 并参考 'pch' 值部分。

英文:

You need to have a point shape that allows both fill and colour.

library(ggplot2)
cars %>%
    ggplot() + 
    geom_point(
        aes(x = speed, y = dist,
            color= I(ifelse(dist >50, 'red', 'black')),
            fill= I(ifelse(dist >50, 'pink', 'gray')), 
        ), 
        shape = 21, 
        size = 4 # changing size so it's easy to visualise
    )

conditionally set both fill and color in geom_point ggplot R

To check point shapes that allow both fill and colour use help(points) and refer to the 'pch' values section

huangapple
  • 本文由 发表于 2023年2月18日 03:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定