如何解决R -ggplot2 带 2 个变量的点图
这是我第三次尝试用 2 个变量制作点状图。
我想要这两个图的组合:
我基本上需要一个点图(我需要 stackdir='center'-function!),但符号类似于那些正方形(2 色)。我尝试了几种变体,在第一个图中使用了带有 position = position_dodge2 的 geom_point,但位置并不令人满意。我需要正方形从中心到两边堆叠起来,并且像点图一样在一条线上。我还需要 0.5 和 -0.5 处的 y 轴截止值。有什么想法吗?
这是我使用的代码:
library(ggplot2)
library(scales)
library(dplyr)
ggplot(df,aes(x = Code,y = Corr)) +
geom_jitter(aes(colour = Vari,fill = Con),shape=22,size = 2,stroke = 1) +
scale_fill_manual(values=c("yellow","black","grey","purple","plum","cyan","dimgrey")) +
scale_colour_manual(values=c("red","dodgerblue","seagreen","orange","deeppink","saddlebrown")) +
theme(
legend.position = "right",axis.text.x = element_text(angle = 90,vjust = 0.6),strip.text.y = element_blank())
点图:
df%>%
mutate(bin = Corr < 0) %>%
# plot the data using a facet_grid with free y scales
ggplot(aes(x = Code,y = Corr,fill = Vari)) +
facet_grid(bin ~ .,scale='free_y') +
theme(legend.position="bottom") +
geom_dotplot(binaxis='y',stackdir='center',dotsize = 1.0) +
theme(axis.text.x = element_text(angle=90,vjust=0.6)) +
theme(strip.text.y = element_blank()) +
scale_fill_manual(values=c("yellow","red","magenta","lightblue","blue","lawngreen","limegreen","darkolivegreen","peru","tan4","dimgrey"))
还有我的示例 df:
df <- structure(list(Vari = c(
"PM","TMK","VPM","TXK","TNK","TGK","TGK"
),Code = c(
"R10","J06","J20-J22","J20","H00-H06","B85-B89","I20-I25","I20-I25"
),Corr = c(
-0.569,-0.5125,-0.5739,-0.5843,-0.5603,-0.5744,-0.5547,-0.6168,-0.5897,-0.5458,-0.5867,-0.5628,-0.5047,-0.5086,-0.5172,-0.512,-0.5229,-0.5257,-0.6172,-0.6003,-0.5599,-0.602,-0.5912,-0.5032,-0.5121,-0.5187,-0.5966,-0.59,-0.5661,-0.5879,-0.5589,0.5104,0.5758,0.5491,0.528,-0.5153,-0.5516
),Con = c(
"Hi","Eg","WFX","WFM","No2","PM25","MAM","Hi","Hi"
)),row.names = c(
23L,35L,36L,37L,38L,39L,40L,43L,44L,45L,46L,47L,50L,51L,52L,53L,54L,55L,58L,59L,60L,61L,62L,64L,65L,66L,67L,68L,69L,70L,71L,74L,75L,86L,87L,100L,101L
),class = "data.frame")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。