首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R- ggplot geom_dotplot形状选项

R- ggplot geom_dotplot形状选项
EN

Stack Overflow用户
提问于 2016-01-09 23:31:11
回答 1查看 3.7K关注 0票数 9

我想使用geom_dotplot根据点的形状来区分两个不同的变量(而不是文档中建议的颜色)。例如:

代码语言:javascript
复制
library(ggplot2)
set.seed(1)
x = rnorm(20)
y = rnorm(20)
df = data.frame(x,y)
ggplot(data = df) + 
   geom_dotplot(aes(x = x), fill = "red") + 
   geom_dotplot(aes(x=y), fill = "blue")

即区分下面示例中的x和y

我想把所有的x都设为点,y设为三角形。

这个是可能的吗?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2017-12-16 05:28:07

你也许可以使用geom_dotplot加上base R的条形图函数来拼凑出与你想要的东西类似的东西。

代码语言:javascript
复制
#Save the dot plot in an object.

dotplot <- ggplot(data = df) +
geom_dotplot(aes(x = x), fill = "red") +
geom_dotplot(aes(x=y), fill = "blue")

#Use ggplot_build to save information including the x values.
dotplot_ggbuild <- ggplot_build(dotplot)

main_info_from_ggbuild_x <- dotplot_ggbuild$data[[1]]
main_info_from_ggbuild_y <- dotplot_ggbuild$data[[2]]

#Include only the first occurrence of each x value.

main_info_from_ggbuild_x <- 
main_info_from_ggbuild_x[which(duplicated(main_info_from_ggbuild_x$x) == FALSE),]

main_info_from_ggbuild_y <-
main_info_from_ggbuild_y[which(duplicated(main_info_from_ggbuild_y$x) == FALSE),]

#To demonstrate, let's first roughly reproduce the original plot.

stripchart(rep(main_info_from_ggbuild_x$x,
times=main_info_from_ggbuild_x$count),
pch=19,cex=2,method="stack",at=0,col="red")      

stripchart(rep(main_info_from_ggbuild_y$x,
times=main_info_from_ggbuild_y$count),
pch=19,cex=2,method="stack",at=0,col="blue",add=TRUE)

代码语言:javascript
复制
#Now, redo using what we actually want.
#You didn't specify if you want the circles and triangles filled or not. 
#If you want them filled in, just change the pch values.

stripchart(rep(main_info_from_ggbuild_x$x,
times=main_info_from_ggbuild_x$count),
pch=21,cex=2,method="stack",at=0)

stripchart(rep(main_info_from_ggbuild_y$x,
times=main_info_from_ggbuild_y$count),
pch=24,cex=2,method="stack",at=0,add=TRUE)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34695363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档