英文:
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'))
)
)
答案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
)
To check point shapes that allow both fill and colour use help(points)
and refer to the 'pch' values section
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论