我使用geom_pointfrom 制作了一个数字ggplot2(只显示了它的一部分).颜色代表3个类.黑条是卑鄙的(与问题无关).
数据结构如下(存储在列表中):
V1 V2 V3
1 L. brevis 5 class1
3 L. sp. 13 class1
4 L. rhamnosus 14 class1
5 L. lindneri 17 class1
6 L. plantarum 17 class1
7 L. acidophilus 18 class1
8 L. acidophilus 18 class1
10 L. plantarum 18 class1
... ... .. ...
Run Code Online (Sandbox Code Playgroud)
V2数据点在y轴上的位置在哪里,V3是类(颜色).
现在我想在图中显示三个类中每个类的百分比(或者甚至可以作为饼图:-)).我在图像上为"嗜酸乳杆菌"做了一个例子(66.7%/ 33.3%).
理想情况下解释组的图例也由R生成,但我可以手动完成.
我怎么做?
忘了在"L. acidophilus"栏上添加第3组的0%...对不起.
编辑:这里的ggplot2代码:
p <- ggplot(myData, aes(x=V1, y=V2)) +
geom_point(aes(color=V3, fill=V3), size=2.5, cex=5, shape=21, stroke=1) +
scale_color_manual(values=colBorder, labels=c("Class I","Class II","Class III","This study")) +
scale_fill_manual(values=col, labels=c("Class I","Class II","Class III","This study")) +
theme_bw() +
theme(axis.text.x=element_text(angle=50,hjust=1,face="italic", color="black"), text = element_text(size=12),
axis.text.y=element_text(color="black"), panel.grid.major = element_line(color="gray85",size=.15), panel.grid.minor = element_blank(),
panel.grid.major.y = element_blank(), axis.ticks = element_line(size = 0.3), panel.border = element_rect(fill=NA, colour = "black", size=0.3)) +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95, colour="black", geom="point") +
guides(fill=guide_legend(title="Class", order=1), color=guide_legend(title="Class",order=1), shape=guide_legend(title="Blup", order=2))
Run Code Online (Sandbox Code Playgroud)
C8H*_*4O2 13
为此,您可以使用二次X轴(新GGPLOT2 V2.2.0),但很难与x轴的分类变量做的,因为它不与工作scale_x_discrete()而已,scale_x_continuous().因此,您必须将因子转换为整数,基于此绘图,然后覆盖主x轴上的标签.
例如:
set.seed(123)
df <- iris[sample.int(nrow(iris),size=300,replace=TRUE),]
# Assume we are grouping by species
# Some group-level stats -- how about count and mean/sdev of sepal length
library(dplyr)
df_stats <- df %>%
group_by(Species) %>%
summarize(stat_txt = paste0(c('N=','avg=','sdev='),
c(n(),round(mean(Sepal.Length),2),round(sd(Sepal.Length),3) ),
collapse='\n') )
library(ggplot2)
ggplot(data = df,
aes(x = as.integer(Species),
y = Sepal.Length)) +
geom_point() +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95,
colour="black", geom="point") +
theme_bw() +
scale_x_continuous(breaks=1:length(levels(df$Species)),
limits = c(0,length(levels(df$Species))+1),
labels = levels(df$Species),
minor_breaks=NULL,
sec.axis=sec_axis(~.,
breaks=1:length(levels(df$Species)),
labels=df_stats$stat_txt)) +
xlab('Species') +
theme(axis.text.x = element_text(hjust=0))
Run Code Online (Sandbox Code Playgroud)
grid.arrange您的统计数据作为主图表顶部的单独图表.这有点简单,但两个图表并不完全排列,可能是因为在顶部图表的轴上抑制了刻度和标签.
library(ggplot2)
library(gridExtra)
p <-
ggplot(data = df,
aes(x = Species,
y = Sepal.Length)) +
geom_point() +
stat_summary(aes(shape="mean"), fun.y=mean, size = 6, shape=95,
colour="black", geom="point") +
theme_bw() +
theme(axis.text.x = element_text(angle=45, hjust=1, vjust=1))
annot <-
ggplot(data=df_stats, aes(x=Species, y = 0)) +
geom_text(aes(label=stat_txt), hjust=0) +
theme_minimal() +
scale_x_discrete(breaks=NULL) +
scale_y_continuous(breaks=NULL) +
xlab(NULL) + ylab('')
grid.arrange(annot, p, heights=c(1,8))
Run Code Online (Sandbox Code Playgroud)