Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r中带4个变量的分组条形图_R_Bar Chart - Fatal编程技术网

r中带4个变量的分组条形图

r中带4个变量的分组条形图,r,bar-chart,R,Bar Chart,我是r的初学者,我一直在尝试如何绘制这个图形 我有4个变量(五个地方的砾石百分比、沙子百分比、淤泥百分比)。我试图绘制这3种类型的沉积物在每个站点(x)中的百分比(y)。在x轴上有五组,每组3根 Station % gravel % sand % silt 1 PRA1 28.430000 70.06000 1.507000 2 PRA3 19.515000 78.07667 2.406000 3 PRA4 19.77

我是r的初学者,我一直在尝试如何绘制这个图形

我有4个变量(五个地方的砾石百分比、沙子百分比、淤泥百分比)。我试图绘制这3种类型的沉积物在每个站点(x)中的百分比(y)。在x轴上有五组,每组3根


   Station   % gravel    % sand      % silt
1   PRA1    28.430000   70.06000    1.507000
2   PRA3    19.515000   78.07667    2.406000
3   PRA4    19.771000   78.63333    1.598333
4   PRB1    7.010667    91.38333    1.607333
5   PRB2    18.613333   79.62000    1.762000 

我试着绘制一个分组条形图

grao <- read_excel("~/Desktop/Masters/Data/grao.xlsx")
colors <- c('#999999','#E69F00','#56B4E9','#94A813','#718200')
barplot(table(grao$Station, grao$`% gravel`, grao$`% sand`, grao$`% silt`), beside = TRUE, col = colors)

但它创造了一个疯狂的图形

有人能帮帮我吗?我已经被困在这里好几个星期了


干杯

我想这可能就是你想要的:

#install.packages("tidyverse")
library(tidyverse)
df <-  data.frame(
  station = c("PRA1", "PRA3", "PRA4", "PRB1", "PRB2"),
  gravel = c(28.4, 19.5, 19.7, 7.01, 18.6),
  sand = c(70.06, 78.07, 78.63, 91, 79),
  silt = c(1.5, 2.4, 1.6, 1.7, 1.66)
)

df2 <- df %>% 
  pivot_longer(cols = c("gravel", "sand", "silt"), names_to = "Sediment_Type", values_to = "Percentage")

ggplot(df2) +
  geom_bar(aes(x = station, y = Percentage, fill = Sediment_Type ), stat = "identity", position = "dodge") +
theme_minimal() #theme_minimal() is from the ggthemes package
#安装程序包(“tidyverse”)
图书馆(tidyverse)

df
条形图
需要一个
矩阵
,最好是两个维度名称。您可以这样转换数据(删除第一列,同时将其用作行名称):

我建议您使用适当的变量名,因为特殊字符不是最好的主意

colnames(dat) <- c("gravel", "sand", "silt")
dat
#         gravel     sand     silt
# PRA1 28.430000 70.06000 1.507000
# PRA3 19.515000 78.07667 2.406000
# PRA4 19.771000 78.63333 1.598333
# PRB1  7.010667 91.38333 1.607333
# PRB2 18.613333 79.62000 1.762000


数据:

grao
dat <- `rownames<-`(as.matrix(grao[,-1]), grao[,1])
# dat <- xtabs(cbind(X..gravel, X..sand, X..silt) ~ Station, grao)  ## alternatively
colnames(dat) <- c("gravel", "sand", "silt")
dat
#         gravel     sand     silt
# PRA1 28.430000 70.06000 1.507000
# PRA3 19.515000 78.07667 2.406000
# PRA4 19.771000 78.63333 1.598333
# PRB1  7.010667 91.38333 1.607333
# PRB2 18.613333 79.62000 1.762000
.col <- c('#E69F00','#56B4E9','#94A813')  ## pre-define colors
barplot(t(dat), beside=T, col=.col, ylim=c(0, 100),  ## barplot
        main="Here could be your title", xlab="sample", ylab="perc.")
legend("topleft", colnames(dat), pch=15, col=.col, cex=.9, horiz=T, bty="n")  ## legend
box()  ## put it in a box
grao <- read.table(text="   Station   '% gravel'    '% sand'      '% silt'
1   PRA1    28.430000   70.06000    1.507000
2   PRA3    19.515000   78.07667    2.406000
3   PRA4    19.771000   78.63333    1.598333
4   PRB1    7.010667    91.38333    1.607333
5   PRB2    18.613333   79.62000    1.762000 ", header=TRUE)