我似乎无法在该剧情中出现图例。论坛和网络教程尚未提供解决方案。任何帮助将不胜感激。
我创建了一个条形图,当包含和排除NA值时,它会覆盖频率,但是我需要为该图提供图例。
Enter
答案 0 :(得分:0)
首先以整洁的方式转换数据(每行一个观察):
library(tidyverse)
Full_Freq <- Full_Freq %>%
gather(key = IncExc, value = x, -code)
然后情节
ggplot(Full_Freq) +
geom_col(aes(x = code, y = x, fill = IncExc)) +
ylab("Percentage")+
xlab("")+
ggtitle("Tooth and Full Phenotype Code")+
theme_classic()+
theme(plot.title = element_text(hjust= 0.5))+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
theme(legend.position = "right")
您可能需要更改颜色。
答案 1 :(得分:0)
在许多情况下,使用长数据格式可以更轻松地进行绘图。您可以使用reshape2
(也可以使用tidyverse
将数据转换为长格式,如另一个答案所示):
library(reshape2)
# your code here
# reshaping your data into long format
Full_Freq.m <- melt(Full_Freq,id.vars = "code")
Full_Freq.m
# code variable value
#1 RI1 - R1,C1,A,G,R NA_INC 11.800
#2 RI2 - R1,C1,A,E,R NA_INC 10.100
#3 RC1 - R1,C1,A,E,R NA_INC 11.900
#...
# 11 RI1 - R1,C1,A,G,R NA_EXC 54.900
# 12 RI2 - R1,C1,A,E,R NA_EXC 38.600
# 13 RC1 - R1,C1,A,E,R NA_EXC 27.900
# 14 RP3 - R2,C2,B1L1,BGLG,BRLR NA_EXC 34.700
#...
然后进行绘图变得更加容易。 用堆积条形图绘制:
# Making the plot
ggplot(data=Full_Freq.m, aes(x=code, y=value, fill=variable)) + geom_bar(stat="identity")+
ylab("Percentage")+
xlab("") +
ggtitle("Tooth and Full Phenotype Code") +
theme_classic() +
theme(plot.title = element_text(hjust= 0.5))+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
scale_fill_manual(values=c("grey", "black"))
具有闪避条的图:
# dodged bar plot, e.g. bars next to each other
ggplot(data=Full_Freq.m, aes(x=code, y=value, fill=variable)) + geom_bar(stat="identity", position="dodge")+
ylab("Percentage")+
xlab("") +
ggtitle("Tooth and Full Phenotype Code") +
theme_classic() +
theme(plot.title = element_text(hjust= 0.5))+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
scale_fill_manual(values=c("grey", "black"))
覆盖有 条的图:
# bars overlaid
ggplot(data=Full_Freq.m, aes(x=code, y=value, fill=variable)) + geom_bar(stat="identity", position="identity")+
ylab("Percentage")+
xlab("") +
ggtitle("Tooth and Full Phenotype Code") +
theme_classic() +
theme(plot.title = element_text(hjust= 0.5))+
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
scale_fill_manual(values=c('NA_EXC' = 'gray', 'NA_INC' = 'black')) +
geom_col(data=Full_Freq.m[which(Full_Freq.m$variable=="NA_INC"),])
答案 2 :(得分:0)
感谢所有提供帮助的人。我已经整理好代码,减去在x轴上有序的变量。这是我的做法-
````
### Barplot of full phenotype codes that have contrast frequencies when NA values are included/excluded
Full_Freq<-data.frame(NA_INC = c(11.8, 10.1, 11.9, 18.8, 17.8, 1.7, 1.7, 1.8, 3.5, 0.001),
NA_EXC = c(54.9, 38.6, 27.9, 34.7, 36.1, 2.3, 2.3, 2.8, 8.4, 100.0),
code = c("RI1 - R1,C1,A,G,R", "RI2 - R1,C1,A,E,R", "RC1 - R1,C1,A,E,R",
"RP3 - R2,C2,B1L1,BGLG,BRLR", "RP4 - R1,C1,A,P,O", "RM1 A - R3,C3,M1D1L1,MWDELE,MRDRLR",
"RM1 B - R3,C4,M2D1L1,MWDPLP,MRDRLO","RM2 - R3,C3,M1D1L1,MWDGLG,MRDRLR",
"RM3 - R3,C3,M1D1L1,MWDGLG,MRDRLR","RM4 - R1,C1,A,P,R"))
#### make the plot
ggplot(Full_Freq) +
geom_col(aes(x = code, y = NA_EXC, fill = "NA excluded")) +
geom_col(aes(x = code, y = NA_INC, fill= "NA included")) +
scale_fill_manual(name = 'NA Values', values = c('NA excluded' = 'gray', 'NA included' = 'black')) +
ylab("Percentage")+
xlab("")+
ggtitle("Full Phenotype Code for Maxillary Teeth")+
theme_classic()+
theme(plot.title = element_text(hjust= 0.5))+
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust=0.5))
````
答案 3 :(得分:0)
添加到@Oka解决方案:
tidyr::gather()
reorder()
代码
Full_Freq %>%
tidyr::gather("variable", "value", contains("NA")) %>%
ggplot(aes(x=reorder(code, value, max), y=value, fill=variable)) + geom_bar(stat="identity", position="dodge")+
labs(fill = "legend title",
x= "x title",
y = "Percentage",
title = "Tooth and Full Phenotype Code")+
theme_classic() +
theme(axis.text.x = element_text(angle = 90, vjust=0.5)) +
scale_fill_manual(values=c("grey", "black"))