我尝试在ggplot中绘制条形图,在x个国家/地区,每种物种的y种动物的数量为y。我已经设法解决了,但是当我尝试勾勒出每个物种和条形时,我得到了图中每个值之间的边界。
我还尝试使用reprex包创建一个外观更好的问题,包括我的图表,但我的声誉太低,无法明显地张贴这些图片。
所以我只尝试代码:
创建数据框library(tidyverse)
country <- c( "AA", "AA", "BB", "BB", "CC", "CC", "DD", "DD", "EE", "EE")
sheep <-c(130, 146, 12, 15, 19, 0, 44, 57, 99, 123)
cattle <- c(11, 34, 221, 0, 91, 49, 33, 28, 19, 10)
pigs <- c(55, 0, 34, 48, 54, 0, 33, 59, 112, 23)
animals_wide <- data_frame(country, sheep, pigs, cattle)
从宽到长“整形”桌子(tidyr :: gather)
animals_long <- animals_wide %>%
gather(key = species, value = numbers, -country)
glimpse(animals_long)
ggplot绘制
ggplot(animals_long, aes(country, numbers, fill = species)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(values=c("gray", "black", "white"))
尝试通过添加geom_bar(...,color =“ black)将黑色轮廓添加到bar中的'species'
ggplot(animals_long, aes(country, numbers, fill = species)) +
geom_bar(stat = "identity", color = "black") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(values=c("gray", "black", "white"))