能否获取 ggplot2 条形图以显示 Y 轴的直接值?
Can get ggplot2 bar chart to display direct values for Y axis?
当我绘制条形图时,图表在 Y 轴上显示了我不理解的值。如何让条形图使用实际值?
#Here is the code for my graph
stock %>%
#Tidy data to be handled correctly
group_by(year) %>%
filter(year == "2017") %>%
pivot_longer(bio_sus:bio_notsus) %>%
mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
#make the graph
ggplot(aes(ocean_whole, value2/100, fill=name)) +
geom_bar(stat = "identity")
当我的值 2 值介于 100 和 - 100 之间时,条形图显示的值介于 2.5 和 -2.5 之间
ocean_sub code year ocean_whole name value value2
<chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
1 Eastern Central Atlantic NA 2017 atlantic bio_sus 57.1 -57.1
2 Eastern Central Atlantic NA 2017 atlantic bio_notsus 42.9 42.9
3 Eastern Central Pacific NA 2017 pacific bio_sus 86.7 -86.7
4 Eastern Central Pacific NA 2017 pacific bio_notsus 13.3 13.3
5 Eastern Indian Ocean NA 2017 indian bio_sus 68.6 -68.6
如何让图表显示实际值?
#My code is from TidyTuesdays Global seafood:
stock <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/fish-stocks-within-sustainable-levels.csv')
#transformed in the following way
oceans <- c("pacific", "atlantic", "indian", "mediterranean")
lu <- stack(sapply(oceans, grep, x = stock$entity, ignore.case = TRUE))
stock$oceans <- stock$entity
stock$oceans[lu$values] <- as.character(lu$ind)
stock %>%
group_by(oceans) %>%
summarise(across(matches("^share"), sum))
colnames(stock) <- (c("ocean_sub", "code", "year", "bio_sus", "bio_notsus", "ocean_whole"))
在您将 ggplot()
交给 ggplot()
之前,您的小标题包含 ocean_whole
的多个值。这些值的总和等于意外值。检查:
stock %>%
group_by(year) %>%
filter(year == "2017") %>%
pivot_longer(bio_sus:bio_notsus) %>%
mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
group_by(ocean_whole, name) %>%
summarise(sum(value2))
当我绘制条形图时,图表在 Y 轴上显示了我不理解的值。如何让条形图使用实际值?
#Here is the code for my graph
stock %>%
#Tidy data to be handled correctly
group_by(year) %>%
filter(year == "2017") %>%
pivot_longer(bio_sus:bio_notsus) %>%
mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
#make the graph
ggplot(aes(ocean_whole, value2/100, fill=name)) +
geom_bar(stat = "identity")
当我的值 2 值介于 100 和 - 100 之间时,条形图显示的值介于 2.5 和 -2.5 之间
ocean_sub code year ocean_whole name value value2
<chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
1 Eastern Central Atlantic NA 2017 atlantic bio_sus 57.1 -57.1
2 Eastern Central Atlantic NA 2017 atlantic bio_notsus 42.9 42.9
3 Eastern Central Pacific NA 2017 pacific bio_sus 86.7 -86.7
4 Eastern Central Pacific NA 2017 pacific bio_notsus 13.3 13.3
5 Eastern Indian Ocean NA 2017 indian bio_sus 68.6 -68.6
如何让图表显示实际值?
#My code is from TidyTuesdays Global seafood:
stock <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-10-12/fish-stocks-within-sustainable-levels.csv')
#transformed in the following way
oceans <- c("pacific", "atlantic", "indian", "mediterranean")
lu <- stack(sapply(oceans, grep, x = stock$entity, ignore.case = TRUE))
stock$oceans <- stock$entity
stock$oceans[lu$values] <- as.character(lu$ind)
stock %>%
group_by(oceans) %>%
summarise(across(matches("^share"), sum))
colnames(stock) <- (c("ocean_sub", "code", "year", "bio_sus", "bio_notsus", "ocean_whole"))
在您将 ggplot()
交给 ggplot()
之前,您的小标题包含 ocean_whole
的多个值。这些值的总和等于意外值。检查:
stock %>%
group_by(year) %>%
filter(year == "2017") %>%
pivot_longer(bio_sus:bio_notsus) %>%
mutate(value2 = ifelse(name=="bio_sus",-1*value, value)) %>%
group_by(ocean_whole, name) %>%
summarise(sum(value2))