Search code examples
rggplot2subsetdplyrparentheses

R . How to subsetting a row which have () parentheses in the name?


I am very noob to R, and I am trying a plot.

The file contain a bunch of rows with 4 different names, I can subsetting someones, but I have problems with others which have parenthesis in the text. How I can subsetting theses?

I am trying to reorder too, but i can`t

Thank you much in advance!

The file is this : link to .csv

library(tidyverse)

dfagro<- read.csv("C:/Users/../data.csv", encoding = "ASCII", header = TRUE, sep = ",")
colnames(dfagro) <- c("time","GEO", "crop", "stuc", "Value", "flag")

# dfagro$Value = as.numeric(gsub(",","\\.",dfagro$Value))

# dfagro %>%
# mutate(GEO = fct_reorder(GEO, Value)) %>%

ggplot(subset(dfagro, crop %in% c("Permanent crops for human consumption")), aes(x=GEO, y=Value)) +
     geom_bar(stat="identity", width=0.6) + coord_flip()      # +
  #geom_text(data=dfagro, aes(y=Value,label=Value),vjust=1)

the problem came when I try to plot this kind of rows with ():

# ggplot(subset(dfagro, crop %in% c("Fresh vegetables (including melons)")), aes(x = GEO, y = Value))+

Solution

  • We could use filter

    library(dplyr)
    dfagro %>%
            mutate(GEO = fct_reorder(GEO, Value)) %>%
            filter(crop %in% "Permanent crops for human consumption")