我的条形图只打印数据中的一个条形图

My bar chart only prints a single bar from my data

当使用以下数据和代码时,我的条形图字符只打印 1 个条形图,知道如何解决这个问题吗?

life_bar
# A tibble: 6 × 2
  Continent                mn
  <chr>                 <dbl>
1 Africa                 41.1
2 Americas               19.5
3 Eastern Mediterranean  47.0
4 Europe                 15.6
5 South-East Asia        37.7
6 Western Pacific        16.4

使用代码;

ggplot(life_bar, aes(x = 'mn')) +
geom_bar(fill = 'green', col = 'black')

您需要同时指定 xy 变量。 x 应该是 Continenty 应该是 mn。您不引用列名。

此外,在使用 geom_bar() 时,您需要指定应如何聚合值 - 在您的情况下 geom_bar(stat = "identity")。您可以通过使用 geom_col().

来避免这种情况
ggplot(life_bar, aes(x = Continent, y = mn)) + 
geom_col()

R 中有很多 built-in 帮助 例如 ?geom_barextensive online help too.

可能的解决方案:

示例数据:

life_bar <- structure(list(...1 = c(1, 2, 3, 4, 5, 6), Continent = c("Africa", 
"Americas", "Eastern Mediterranean", "Europe", "South-East Asia", 
"Western Pacific"), mn = c(41.1, 19.5, 47, 15.6, 37.7, 16.4)), spec = structure(list(
    cols = list(...1 = structure(list(), class = c("collector_double", 
    "collector")), Continent = structure(list(), class = c("collector_character", 
    "collector")), mn = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x0a577cf8>, row.names = c(NA, 
-6L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))

示例代码:

(plot<-ggplot(life_bar, aes(x=Continent, y=mn, fill=Continent)) +  
    geom_col(position="dodge", color="black")+
    geom_text(aes(label = mn), 
              position = position_dodge(width=1),
              vjust=1.5, size=5, face="bold")+
    
    labs(x="", y="mn", title="Life bar", fill="")+
    # facet_wrap(~variable)+
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5,  face="bold", size=20, color="black")) + 
    theme(axis.title.x = element_text(family="Times", face="bold", size=16, color="black"))+
    theme(axis.title.y = element_text(family="Times", face="bold", size=16, color="black"))+
    theme(axis.text.x = element_text( hjust = 1,  face="bold", size=14, color="black") )+
    theme(axis.text.y = element_text( hjust = 1,  face="bold", size=14, color="black") )+
    theme(plot.title = element_text(hjust = 0.5)))

剧情: