ggplot如何从两个不同的列中选择值?

问题描述 投票:0回答:2

我有一个.csv文件,其中每个项目都有3个参数。如果项目为"apple",则3个值是:

  • “”收获面积“
  • “”收益“
  • “生产”

我不知道如何仅使用“生产”值来绘制这些苹果行

这是我的代码,但是它绘制了三个值:

library(tidyverse)
library(dplyr)
df <- read.csv("C:/Users/....data.csv",
  encoding = "ASCII", header = TRUE, 
  sep = ","
)


ggplot(subset(df, Item == "Chillies and peppers, green"),
             aes(x = Area, y = Y2014)) +
  geom_bar(stat = "identity", width = 0.6) +
  coord_flip()

view(df)

这是.csv link to csv

非常感谢!

r csv ggplot2 charts subset
2个回答
0
投票

因为已经加载了dplyr,所以我们要使用它,特别是filter

europe %>% 
  filter(Item == "Apples",
         Element == "Production") %>% 
  ggplot(aes(x = Area, y = Y2014)) +
  geom_bar(stat = "identity", width = 0.6) +
  coord_flip()

enter image description here


0
投票

您可以在调用ggplot之前过滤数据,像这样

df %>%
    filter(Item == "Apples",
           Element == "Production") %>%
    ggplot() +
    geom_bar(aes(Area, Y2014),
             stat = "identity", width = 0.6) +
  coord_flip()

注意:您也可以使用reorder(Area, Y2014, FUN = abs)而不是简单的Area

df %>%
    filter(Item == "Apples",
           Element == "Production") %>%
    ggplot() +
    geom_bar(aes(reorder(Area, Y2014, FUN = abs),
                 Y2014),
             stat = "identity", width = 0.6) +
  coord_flip()
© www.soinside.com 2019 - 2024. All rights reserved.