如何使用 ggplot 为多列创建条形图?
How to create barplot for more than one column using ggplot?
如何使用 ggplot 向条形图添加多列?
df = data.frame(c("player 1", "player 2", "player 3"),c(1,2,3), c(5,6,7), c(8,9,10))
names(df) = c("player","game 1","game 2", "game 3")
player game 1 game 2 game 3
-----------------------------------
player 1 1 5 8
player 2 2 6 9
player 3 3 7 10
barplot(colSums(df[,2:4]))
在 gglot 中,我不确定如何添加多个 x 轴美学,而 y 轴是它们的总和。例如,对于上面的数据框,我需要创建一个包含三个 x 轴变量的条形图作为游戏 1、游戏 2 和游戏 3,并为每个条添加一个标签,该标签是玩家每个 x 轴变量的总和。
请帮忙。
一般来说,ggplot 最适合长数据。所以延长它的旋转时间(即将游戏变量组合成一个变量)。我假设你想要分组的条形图,所以你可能想为此使用 fill = player
。
library(ggplot2)
library(dplyr)
library(tidyr)
dat <- data.frame(
player = c("player 1", "player 2", "player 3"),
game1 = c(1,2,3),
game2 = c(5,6,7),
game3 = c(8,9,10)
)
dat %>%
pivot_longer(cols = contains("game"),
names_to = "game") %>%
ggplot(aes(x = game, y = value, fill = player)) +
geom_col(position = "dodge")
如果我没有理解错的话,这就是你需要的。但首先,不建议在列名中使用空格,所以我重命名了你的 df.试试这些选项:
library(tidyverse)
library(magrittr)
df <- data.frame(player = c("player 1", "player 2", "player 3"),
game1 = c(1,2,3),
game2 = c(5,6,7),
game3 = c(8,9,10))
option1 <-
df %>%
pivot_longer(cols = -player,
names_to = 'Games',
values_to = 'Scores') %>%
ggplot(aes(x = player,
y = Scores,
color = Games,
fill = Games,
label = Scores)) +
geom_col(position = 'dodge') +
geom_text(position = position_dodge(width = 1),
vjust = -0.5,
size = 7) +
scale_y_continuous(limits = c(0,11),
breaks = seq(0,10,2))
option 1
option2 <-
df %>%
mutate(Total = game1 + game2 + game3) %>%
pivot_longer(cols = -player,
names_to = 'Games',
values_to = 'Scores') %>%
filter(Games == 'Total') %>%
ggplot(aes(x = player,
y = Scores,
color = Games,
fill = Games,
label = Scores)) +
geom_col(show.legend = FALSE) +
geom_text(vjust = -0.5,
size = 7,
show.legend = FALSE) +
scale_y_continuous(limits = c(0,22),
breaks = seq(0,22,2))
option2
希望对您有所帮助。
如何使用 ggplot 向条形图添加多列?
df = data.frame(c("player 1", "player 2", "player 3"),c(1,2,3), c(5,6,7), c(8,9,10))
names(df) = c("player","game 1","game 2", "game 3")
player game 1 game 2 game 3
-----------------------------------
player 1 1 5 8
player 2 2 6 9
player 3 3 7 10
barplot(colSums(df[,2:4]))
在 gglot 中,我不确定如何添加多个 x 轴美学,而 y 轴是它们的总和。例如,对于上面的数据框,我需要创建一个包含三个 x 轴变量的条形图作为游戏 1、游戏 2 和游戏 3,并为每个条添加一个标签,该标签是玩家每个 x 轴变量的总和。
请帮忙。
一般来说,ggplot 最适合长数据。所以延长它的旋转时间(即将游戏变量组合成一个变量)。我假设你想要分组的条形图,所以你可能想为此使用 fill = player
。
library(ggplot2)
library(dplyr)
library(tidyr)
dat <- data.frame(
player = c("player 1", "player 2", "player 3"),
game1 = c(1,2,3),
game2 = c(5,6,7),
game3 = c(8,9,10)
)
dat %>%
pivot_longer(cols = contains("game"),
names_to = "game") %>%
ggplot(aes(x = game, y = value, fill = player)) +
geom_col(position = "dodge")
如果我没有理解错的话,这就是你需要的。但首先,不建议在列名中使用空格,所以我重命名了你的 df.试试这些选项:
library(tidyverse)
library(magrittr)
df <- data.frame(player = c("player 1", "player 2", "player 3"),
game1 = c(1,2,3),
game2 = c(5,6,7),
game3 = c(8,9,10))
option1 <-
df %>%
pivot_longer(cols = -player,
names_to = 'Games',
values_to = 'Scores') %>%
ggplot(aes(x = player,
y = Scores,
color = Games,
fill = Games,
label = Scores)) +
geom_col(position = 'dodge') +
geom_text(position = position_dodge(width = 1),
vjust = -0.5,
size = 7) +
scale_y_continuous(limits = c(0,11),
breaks = seq(0,10,2))
option 1
option2 <-
df %>%
mutate(Total = game1 + game2 + game3) %>%
pivot_longer(cols = -player,
names_to = 'Games',
values_to = 'Scores') %>%
filter(Games == 'Total') %>%
ggplot(aes(x = player,
y = Scores,
color = Games,
fill = Games,
label = Scores)) +
geom_col(show.legend = FALSE) +
geom_text(vjust = -0.5,
size = 7,
show.legend = FALSE) +
scale_y_continuous(limits = c(0,22),
breaks = seq(0,22,2))
option2
希望对您有所帮助。