提问人:Maggie 提问时间:11/6/2023 更新时间:11/6/2023 访问量:74
在 R 的 x 轴上创建一个具有两个类别级别的二维聚簇柱形图,而无需使用 ggplot
create a two dimensional clustered column chart with two levels of categories on the x axis in r without using ggplot
问:
我有一个数据表,可以放入 excel 中的簇状柱形图中。我想做同样的事情,但在 R 中创建图表。这怎么能做到?Excel中的数据图表表
我尝试了以下方法,并产生了附加的图表。数据排序和标记不正确。ggplot 在我的机器上不起作用,所以不是一种选择。
# Create a data frame with the given data
data <- data.frame(
City = c("Minneapolis", "Minneapolis", "Minneapolis", "Orlando", "Orlando", "Orlando",
"Phoenix", "Phoenix", "Phoenix", "Sacramento", "Sacramento", "Sacramento"),
Tree_Type = c("No Tree", "Medium Tree", "Large Tree", "No Tree", "Medium Tree", "Large Tree",
"No Tree", "Medium Tree", "Large Tree", "No Tree", "Medium Tree", "Large Tree"),
E_Plus = c(158, 133, 123, 879, 863, 827, 2858, 2812, 2779, 421, 363, 351),
iTE = c(1354, 1337, 1331, 6057, 6043, 6041, 4356, 4336, 4328, 2125, 2099, 2078)
)
# Set up the positions for bars
bar_width <- 0.35
barplot(
height = c(data$E_Plus, data$iTE),
beside = TRUE,
names.arg = paste(data$City, data$Tree_Type),
col = c("red", "blue"),
main = "Energy Consumption by City and Tree Type",
ylab = "Energy Consumption",
xlab = "City / Tree Type",
ylim = c(0, max(data$E_Plus, data$iTE) + 500) # Adjust the y-axis limits if necessary
)
# Adding legend
legend("topright", legend = c("EnergyPlus", "i-Tree Energy"), fill = c("red", "blue"))
# Adding data labels under the bars
text(
x = 1:(nrow(data) * 2),
y = -50, # Adjust the vertical position of the labels
labels = paste(data$City, data$Tree_Type),
xpd = TRUE,
srt = 45,
adj = c(1.2, 1.2),
cex = 0.8
)
答:
1赞
thothal
11/6/2023
#1
您可以使用 (即提供矩阵而不是向量的形式 - 每列代表一个柱形,每行要么是一个堆叠的条形,要么是一个躲避的条形,旁边是 ()。然后 eahc 组已经很好地组合在一起,组之间的一些间隙。matrix
barplot
height
beside = TRUE
然后,您需要对标签进行一些摆弄,并且可以非常接近 Excel 正在执行的操作:
with(data, {
op <- par(mar = c(7, 4, 4, 2) + .1) ## make some space for the labels below
barplot(rbind(E_Plus, iTE), ## use matrix form
col = c("steelblue", "orange"),
beside = TRUE
)
## Tree_Type below each group
text(seq(2, by = 3, length.out = 12), par("usr")[3] - .25,
srt = 90, adj = 1, xpd = TRUE,
labels = Tree_Type) ## xpd = TRUE to put labels outside the plotting area
## City centered below each group of three Tree_Types
text(seq(5, by = 9, length.out = 4), par("usr")[3] - 1500,
xpd = TRUE, labels = unique(City))
x <- seq(.5, by = 9, length.out = 5)
## lines as in the Excel variant
segments(x, 0, x, par("usr")[3] - 1700, col = "lightgray", xpd = TRUE)
par(op)
})
1赞
jay.sf
11/6/2023
#2
我们可以对数据并使用一些算法来避免硬编码(尽可能)。reshape
r <- reshape(data, idvar='Tree_Type', timevar='City', v.names=c('E_Plus', 'iTE'),
direction='w') |>
{\(.) `rownames<-`(.[, -1], .[, 1])}() |> as.matrix()
sq <- seq_len(ncol(r)); .clr <- c('#2297e6', '#ffa500')
par(mar=c(10, 4, 4, 2))
plot.new()
plot.window(c(2.5, prod(dim(r))*2.15), c(range(r)))
lapply(seq(0, max(r), 1e3), \(i) abline(h=i, col='gray80'))
b <- barplot(unlist(lapply(unique(data$City), \(x) t(r[, grep(x, colnames(r))]))),
space=c(2, .25), col=.clr, legend.text=c('E+', 'iTE'), las=1, add=T)
mtext(rownames(r), 1, 0.5, at=colMeans(matrix(b, 2)), las=2)
mtext(unique(data$City), 1, 6, at=colMeans(matrix(b, 6)), las=1, col=8)
b2 <- matrix(b[-1, ], 2) |> suppressWarnings()
n <- nrow(r)
pos <- colMeans(b2[, seq_len(ncol(b2)) %% n == 0][, 1:n])
.d <- diff(tail(pos, 2))
pos <- c(pos[1] - .d, pos, pos[length(pos)] + .d)
segments(pos, 0, pos, -.26e4, xpd=TRUE, col='gray80')
可惜还没有争论!barplot
panel.first=
数据:
data <- structure(list(City = c("Minneapolis", "Minneapolis", "Minneapolis",
"Orlando", "Orlando", "Orlando", "Phoenix", "Phoenix", "Phoenix",
"Sacramento", "Sacramento", "Sacramento"), Tree_Type = c("No Tree",
"Medium Tree", "Large Tree", "No Tree", "Medium Tree", "Large Tree",
"No Tree", "Medium Tree", "Large Tree", "No Tree", "Medium Tree",
"Large Tree"), E_Plus = c(158, 133, 123, 879, 863, 827, 2858,
2812, 2779, 421, 363, 351), iTE = c(1354, 1337, 1331, 6057, 6043,
6041, 4356, 4336, 4328, 2125, 2099, 2078)), class = "data.frame", row.names = c(NA,
-12L))
评论