library(ggplot2)ggplot(data, aes(x = x_var, y = y_var),color=z_var) + geom_jitter()之后如何分开两组的抖动点?
时间: 2023-06-23 20:09:23 浏览: 149
可以使用 `facet_wrap()` 函数来按照 `z_var` 的不同取值分割成两个子图,每个子图对应一组抖动点。具体的代码如下:
```
ggplot(data, aes(x = x_var, y = y_var, color = z_var)) +
geom_jitter() +
facet_wrap(~ z_var, ncol = 1)
```
其中,`facet_wrap()` 函数中的 `~ z_var` 表示按照 `z_var` 来分割子图,`ncol = 1` 表示每行只有一个子图。这样就可以将两组抖动点分别展示在两个子图中了。
相关问题
library(ggplot2)ggplot(data, aes(x = x_var, y = y_var),color=z_var) + geom_jitter()之后如何在一张图内分开两组的抖动点?
你可以使用 `facet_wrap()` 函数将数据以 `z_var` 为分组条件分为两个子图,然后在每个子图上分别使用 `geom_jitter()` 函数绘制 `x_var` 和 `y_var` 之间的关系。以下是代码示例:
```
ggplot(data, aes(x = x_var, y = y_var, color = z_var)) +
facet_wrap(~ z_var, nrow = 1) +
geom_jitter()
```
这样就会在一张图内分开两组的抖动点。其中 `nrow` 参数表示每行显示的子图数量,这里设为1,也就是只显示一行。你可以根据需要调整这个参数。
data<-melt(mydata,id.vars = 'ID') p <- ggplot(data=data,aes(ID,value,fill=variable)) + geom_bar(stat="identity",position="stack", color="#f0f0f0", width=1,size=0.20) + scale_fill_manual(values=c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99", "#e31a1c","#fdbf6f","#ff7f00","#cab2d6", "#6a3d9a","#ffff99","#ffed6f","#ccebc5", "#bc80bd","#d9d9d9","#fccde5","#b3de69", "#fdb462","#80b1d3","#fb8072","#8dd3c7", "#fdb222","#92b1d0","#fb8098","#9dd3c7")) p+labs(x = "", y = "") + #scale_x_continuous(breaks = seq(min(data$ID), max(data$ID), by = 1)) + # Add this line scale_y_continuous(expand = c(0,0),limits = c(0,100),breaks = seq(0, 100, by = 20)) + theme_classic() + theme( panel.background = element_rect(fill="white", colour="white", size=0.25), axis.line = element_line(colour="black", size=0.5), axis.title = element_text(size=13, color="black"), axis.text = element_text(size=12, color="black"), legend.position = c(1.15, 0.5), #修改图例位置 legend.text = element_text(size =10), aspect.ratio = 1, # set figure size to 8x6 inches plot.title = element_text(size = 10), # optional, add a title axis.text.x = element_text(angle = 45, hjust = 1), # Rotate x-axis labels by 45 degrees legend.box.spacing = unit(0.01, "cm"), # 设置图例中每个元素的间距 #legend.key.width = 0.5, # 缩小图例颜色方块的宽度 legend.background = element_blank() # 设置图例背景为透明 ) + guides(fill = guide_legend(ncol = 1,title="Bac_Phylum")) 修改脚本,图片中顺序与数据顺序一致
你可以尝试以下修改脚本,使得图片中的顺序与数据顺序一致:
```
library(dplyr)
library(ggplot2)
# 对数据进行排序,根据 value 和 variable 列进行升序排序
mydata <- arrange(mydata, value, variable)
# 将数据进行 melt 操作
data <- melt(mydata, id.vars = 'ID')
# 修改颜色顺序与 Bac_Phylum 列中因子的顺序一致
my_colors <- c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99",
"#e31a1c","#fdbf6f","#ff7f00","#cab2d6",
"#6a3d9a","#ffff99","#ffed6f","#ccebc5",
"#bc80bd","#d9d9d9","#fccde5","#b3de69",
"#fdb462","#80b1d3","#fb8072","#8dd3c7",
"#fdb222","#92b1d0","#fb8098","#9dd3c7")
my_colors <- my_colors[match(levels(factor(data$variable)), levels(factor(mydata$Bac_Phylum)))]
# 绘制堆叠条形图
p <- ggplot(data = data, aes(ID, value, fill = variable)) +
geom_bar(stat = "identity", position = "stack", color = "#f0f0f0",
width = 1, size = 0.20) +
scale_fill_manual(values = my_colors) + # 修改颜色顺序
labs(x = "", y = "") +
scale_y_continuous(expand = c(0,0), limits = c(0,100),
breaks = seq(0, 100, by = 20)) +
theme_classic() +
theme(panel.background = element_rect(fill = "white", color = "white", size = 0.25),
axis.line = element_line(colour = "black", size = 0.5),
axis.title = element_text(size = 13, color = "black"),
axis.text = element_text(size = 12, color = "black"),
legend.position = c(1.15, 0.5),
legend.text = element_text(size = 10),
aspect.ratio = 1,
plot.title = element_text(size = 10),
axis.text.x = element_text(angle = 45, hjust = 1),
legend.box.spacing = unit(0.01, "cm"),
legend.background = element_blank()) +
guides(fill = guide_legend(ncol = 1, title = "Bac_Phylum"))
# 输出绘制的图形
p
```
需要注意的是,这段修改后的脚本并没有测试过,如果有错误请及时修改。
阅读全文