r-ggplot无法通过数据帧变量填充绘图



我正试图绘制一个数据集,并根据数据集中的变量填充点;然而,我没有得到以下代码所期望的结果:

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(RColorBrewer)
#> Warning: package 'RColorBrewer' was built under R version 4.0.3
df <- data.frame(
ID   = c("B2008","0E00A", "24101"),
Rate = c(0.12016, 0.12528, 0.24614),
Pos  = c(0.0796, 0.0849, 0.18316),
Neg  = c(0.0249, 0.0249, 0.0925),
Ret  = c(0.0547, 0.06, 0.09066),
EmpCategory = c("Full-Time", "Part-Time", "Un-Employed")
)

head(df, 2)
#>      ID    Rate    Pos    Neg    Ret EmpCategory
#> 1 B2008 0.12016 0.0796 0.0249 0.0547   Full-Time
#> 2 0E00A 0.12528 0.0849 0.0249 0.0600   Part-Time
## Plot Rate vs Return, filled by Employee Category, sized by Negative (Losses)
ggplot(df) +
aes(x=Rate,y=Ret,fill=EmpCategory,size=Neg) +
geom_point() +
xlab("Rate") +
ylab("Return") +
scale_fill_brewer(palette='RdYlGn')

## Plot Rate vs Return, filled by Positive (gains), sized by Employee Category
ggplot(df) +
aes(x=Rate,y=Ret,fill=Pos,size=EmpCategory) +
geom_point() +
xlab("Rate") +
ylab("Return") +
scale_fill_distiller(palette='RdYlGn')
#> Warning: Using size for a discrete variable is not advised.

第一个ggplot调用生成此图
第二次ggplot调用生成此图。

正如你所看到的,无论我使用的是分类变量还是连续变量,我似乎都不能用变量来填充点的颜色。如果能对我所做的错误有所了解,我们将不胜感激。

使用color = EmpCategory而不是fill

有关颜色相关aes的更多信息,请点击此处:https://ggplot2.tidyverse.org/reference/aes_colour_fill_alpha.html

最新更新