我应该使用哪个ggplot2 geom?

时间:2018-03-08 16:24:12

标签: r ggplot2 tidyr

我有一个数据框。

id <- c(1:5)
count_big <- c(15, 25, 7, 0, 12)
count_small <- c(15, 9, 22, 11, 14)
count_black <- c(7, 12, 5, 2, 6)
count_yellow <- c(2, 0, 7, 4, 3)
count_red <- c(8, 4, 4, 2, 5)
count_blue <- c(5, 9, 6, 1, 7)
count_green <- c(8, 9, 7, 2, 5)
df <- data.frame(id, count_big, count_small, count_black, count_yellow, count_red, count_blue, count_green)

如何在ggplot2中显示以下内容以及我应该使用哪个geom:

  • 按ID

  • 细分大小变量
  • 按ID

  • 分类颜色

这只是数据集的一个子集,大约有1000行。

我可以在ggplot2中使用此df,还是需要使用tidyr将其转换为整洁的数据? (不知道data.table)

1 个答案:

答案 0 :(得分:1)

您需要先使用exports = module.exports = functions.https.onRequest(async(req, res) => { cors(req, res, () => {}); await charge_card(req, res); }); 重新构建从长到长的数据。

tidyr

使用library(tidyr) library(ggplot2) df <- gather(df, var, value, starts_with("count")) # remove count_ df$var <- sub("count_", "", df$var) # plot big vs small df_size <- subset(df, var %in% c("big", "small")) ggplot(df_size, aes(x = id, y = value, fill = var)) + geom_bar(stat = "identity", position = position_dodge()) # same routine for colors df_color <- subset(df, !(var %in% c("big", "small"))) ggplot(df_color, aes(x = id, y = value, fill = var)) + geom_bar(stat = "identity", position = position_dodge()) 阻止它进行行计数。 stat = "identity"用于将条形放置在彼此旁边而不是堆叠。