简体   繁体   中英

Passing two variables which are not part of the data frame to facet_grid() in R

I want to pass a variable facet_group_by which is not part of the data frame to the facet_grid() function of the ggplot2-package, but I get the following error:

#> Error in year + month: non-numeric argument for binary operator

This only happens when one of the groupBy List entries consists of more than one element. When I print facet_group_by , and copy paste the console output instead of the variable it works. When using the same output, but putting it into quotes it produces the same error. So I guess the error appears because the variable is a character(?), but I have no clue how to fix this.

library(ggplot2)
library(lubridate)
#> 
#> Attache Paket: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:lubridate':
#> 
#>     intersect, setdiff, union
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(date = sample(seq(as.Date("2017-10-01"), as.Date('2019-12-01'), by="day"), 100),
                 sentiment = sample(c("positive", "negative"), replace = TRUE, 100))
df <- df %>%
  mutate(day = day(as.POSIXlt(date))) %>%
  mutate(week = week(as.POSIXlt(date))) %>%
  mutate(month = month(as.POSIXlt(date))) %>%
  mutate(year = year(as.POSIXlt(date))) %>%
  group_by(sentiment, date, day, month, year) %>%
  summarise(sentiment_count = n())

period <- "day"
groupBy = list(cols = c("year", "month"))
facet_group_by <- paste0(ifelse(length(groupBy[["rows"]]) > 0, paste(groupBy[["rows"]], collapse = " + "),""), " ~ ",
                         ifelse(length(groupBy[["cols"]]) > 0, paste(groupBy[["cols"]], collapse = " + "),""))

# this works
ggplot(data = NULL, aes(x = df, fill = sentiment)) +
  geom_bar(data = subset(df, sentiment == "positive"),aes(x = get(period), y = sentiment_count), stat = "identity") +
  geom_bar(data = subset(df, sentiment == "negative"),aes(x = get(period), y = sentiment_count*(-1)), stat = "identity") +
  facet_grid(~ year + month, scales = "free_x")


# this doesn't work
ggplot(data = NULL, aes(x = df, fill = sentiment)) +
  geom_bar(data = subset(df, sentiment == "positive"),aes(x = get(period), y = sentiment_count), stat = "identity") +
  geom_bar(data = subset(df, sentiment == "negative"),aes(x = get(period), y = sentiment_count*(-1)), stat = "identity") +
  facet_grid(facet_group_by, scales = "free_x")
#> Error in year + month: nicht-numerisches Argument für binären Operator

# this produces the same error
ggplot(data = NULL, aes(x = df, fill = sentiment)) +
  geom_bar(data = subset(df, sentiment == "positive"),aes(x = get(period), y = sentiment_count), stat = "identity") +
  geom_bar(data = subset(df, sentiment == "negative"),aes(x = get(period), y = sentiment_count*(-1)), stat = "identity") +
  facet_grid("~ year + month", scales = "free_x")
#> Error in year + month: nicht-numerisches Argument für binären Operator

Created on 2019-11-29 by the reprex package (v0.3.0)

Halfway there. Wrap your grouping expression into as.formula() and you're good to go:

ggplot(data = NULL, aes(x = df, fill = sentiment)) +
  geom_bar(data = subset(df, sentiment == "positive"),aes(x = get(period), y = sentiment_count), stat = "identity") +
  geom_bar(data = subset(df, sentiment == "negative"),aes(x = get(period), y = sentiment_count*(-1)), stat = "identity") +
  facet_grid(as.formula(facet_group_by), scales = "free_x")

侧面图像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM