提问人:slow_learner 提问时间:10/15/2022 最后编辑:Peterslow_learner 更新时间:10/16/2022 访问量:72
如何在 R 中将两个数据框中的数据绘制为堆积条形图?
How to plot data from two data frames together as a stacked bar chart in R?
问:
我有两个数据帧,分别称为 和 ,其中包含特定地区相应日期的男性和女性的一系列 covid 感染日期和值。我将它们单独绘制为dbDateSubMSortNacional
dbDateSubMSortNacional
contagiosH=barplot(dbDateSubHSortNacional$total[1:5], names.arg = dbDateSubHSortNacional$fecha[1:5], cex.names=0.759, , cex.axis=1, main = "Masculino", xlab="Fechas yy-mm-dd" , ylab="Número de contagios", col = rainbow(5), border = "white", ylim=c(0, 5000))
text(contagiosH, dbDateSubHSortNacional$total[1:5], dbDateSubHSortNacional$total[1:5])
contagiosM=barplot(dbDateSubMSortNacional$total[1:5], names.arg = dbDateSubMSortNacional$fecha[1:5], cex.names=0.759, , cex.axis=1, main = "Femenino", xlab="Fechas yy-mm-dd" , ylab="Número de contagios", col = rainbow(5), border = "white", ylim=c(0, 5000))
text(contagiosM, dbDateSubMSortNacional$total[1:5], dbDateSubMSortNacional$total[1:5])
我已经设法为一个名为datos
barplot(datos, beside=F, col=rainbow(5), legend.text = row.names(datos),
args.legend=list(title ="RRSS preferidas", x="top", inset=c(0, -0.45)))
我试图将这两种方法合并为
barplot(dbDateSubMSortNacional$total[1:5], dbDateSubHSortNacional$total[1:5], names.arg = dbDateSubMSortNacional$fecha[1:5], cex.names=0.759, , cex.axis=1, col = rainbow(5), border = "white", ylim=c(0, 4000))
不幸的是,我得到一个图表。有人可以告诉我如何实现我的目标吗?
编辑:要生成可重复的输出,请使用
fecha <- c("2022-01-01","2022-01-02","2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06", "2022-01-07")
total <- c(1, 3, 5, 7, 9, 11, 13)
dbDateSubMSortNacional <- data.frame(fecha, total);
total <- c(2, 4, 6, 8, 10, 12, 14)
dbDateSubHSortNacional <- data.frame(fecha, total);
@jdobres建议的解决方案存在问题:
返回错误Error in cbind(total.2022 - 1 - 10, total.2022 - 1 - 11, total.2022 - : object 'total.2022' not found
barplot(cbind(total.2022-01-10, total.2022-01-11, total.2022-01-04, total.2022-01-05, total.2022-01-12) ~ set, data = combined_wide, col = rainbow(5))
答:
1赞
jdobres
10/15/2022
#1
首先,您需要将两个数据框合并为一个:
dbDateSubHSortNacional$set <- 'H'
dbDateSubMSortNacional$set <- 'M'
combined <- rbind(dbDateSubHSortNacional, dbDateSubMSortNacional)
fecha total set
1 Monday 2 H
2 Tuesday 4 H
3 Wendesday 6 H
4 Thuersday 8 H
5 Friday 10 H
6 Saturday 12 H
7 Sunday 14 H
8 Monday 1 M
9 Tuesday 3 M
10 Wendesday 5 M
11 Thuersday 7 M
12 Friday 9 M
13 Saturday 11 M
14 Sunday 13 M
如果要使用 绘制此数据,首先将数据转换为“宽”格式会更容易:barplot
reshape
combined_wide <- reshape(combined, direction = 'wide', idvar = 'set', timevar = 'fecha')
set total.Monday total.Tuesday total.Wendesday total.Thuersday total.Friday total.Saturday total.Sunday
1 H 2 4 6 8 10 12 14
8 M 1 3 5 7 9 11 13
然后命令变为:barplot
barplot(cbind(total.Monday, total.Tuesday, total.Wendesday, total.Thuersday, total.Friday) ~ set, data = combined_wide, col = rainbow(5))
您还可以使用 ggplot2 创建类似的图,而无需扩大数据集:
library(ggplot2)
ggplot(data = subset(combined, !(fecha %in% c('Saturday', 'Sunday'))), aes(x = set, y = total, fill = fecha)) +
geom_col()
评论
0赞
slow_learner
10/16/2022
谢谢你的回答。但是,我有一个问题,在我的原始数据中,数据帧的名称定义为 yyyy-mm-dd,因此,我必须写入才能到达绘图,并且我收到错误:.我会附上这个问题的截图,希望你能再次帮助我。barplot(cbind(total.2022-01-10, total.2022-01-11, total.2022-01-04, total.2022-01-05, total.2022-01-12) ~ set, data = combined_wide, col = rainbow(5))
Error in cbind(total.2022 - 1 - 10, total.2022 - 1 - 11, total.2022 - : object 'total.2022' not found
1赞
Peter
10/16/2022
#2
A 和解决方案,后跟一个基本 R 条形图选项:dplyr
ggplot
library(dplyr, warn = FALSE)
library(tidyr)
library(ggplot2)
df1 <- bind_rows(dbDateSubHSortNacional, dbDateSubMSortNacional)
ggplot(df1)+
geom_col(aes(x = set, y = total, fill = fecha))
#get data into wide format:
df2 <-
df1 |>
pivot_wider(names_from = fecha, values_from = total)
barplot(cbind(`2022-01-10`, `2022-01-11`, `2022-01-04`,`2022-01-05`, `2022-01-12`, `2022-01-17`, `2021-12-19`)~set,
data = df2 ,
xlim = c(0,1),
width = 0.30,
col = rainbow(7),
xlab = "Sex",
ylab = "Frequency",
legend.text = colnames(df2)[-1],
args.legend = list(x = "topright"))
创建于 2022-10-15 使用 reprex v2.0.2
数据
fecha <- c("2022-01-10","2022-01-11","2022-01-04","2022-01-05", "2022-01-12","2022-01-17", "2021-12-19")
total <- c(1, 3, 5, 7, 9, 11, 13)
dbDateSubMSortNacional <- data.frame(fecha, total)
total <- c(2, 4, 6, 8, 10, 12, 14)
dbDateSubHSortNacional <- data.frame(fecha, total);
dbDateSubHSortNacional$set <- 'H'
dbDateSubMSortNacional$set <- 'M'
评论
redFav
datos
dbDateSubHSortNacional
datos
-
fecha
c("2022-01-10","2022-01-11","2022-01-04",...)
c("Monday","Tuesday","Wednesday",...