0

I am looking to lay over multiple years off data, based off of weeks. I am looking for the most recent year (2020) to be a bar chat, and the prior years (2019 & 2018) to be line plots.

My current code produces the visual below

data %>%
  ggplot(aes(week, result, fill = year)) +
  geom_col()

Chart

If I wanted to make all the years a bar chart, I know I could use "dodge" in geom_col. However, I am trying to split out the data into different geoms

When I use the subset function (code below), something strange happens with my chart and I can't figure out what is going on

data %>%
  ggplot(aes(week, result, fill = year)) +
  geom_col(data = subset(data, year == 2020)) +
  geom_line(data = subset(data, year != 2020))

Chart 2

Below is a sample of the data that I am using. Any ideas on what I'm doing wrong?

structure(list(year = c("2018", "2018", "2019", "2019", "2020", 
"2020"), week = c(1L, 2L, 1L, 2L, 1L, 2L), result = c(6.88475831020016, 
7.62267452779933, 3.67553313593328, 6.18354398162893, 2.38101968527051, 
5.9596355812872)), .Names = c("year", "week", "result"), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))
mf17
  • 91
  • 6
  • 1
    Hi mf17 and welcome to SO ! Instead of posting an image of your data, it's better to provide a reproducible example of your dataset by following this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. It makes things easier for people trying to help you – dc37 Feb 20 '20 at 01:38
  • 1
    The years in your data are characters, but you're subsetting based on comparing to a number – camille Feb 20 '20 at 01:52

1 Answers1

0

I am not sure where is your error but with my dummy example mimicking your example, I just add color in aes in order to make lines being grouped by years and the rest of the code is similar as yours:

ggplot(df, aes(x = week, y = Value, 
               color = as.factor(year),
               fill = as.factor(year)))+
  geom_col(data = subset(df, year == 2020)) +
  geom_line(data = subset(df, year != 2020))

enter image description here

Data

df <- data.frame(year = rep(2018:2020, each = 7),
                 week = rep(1:7,3),
                 Value = rnorm(21))

EDIT with OP data

Based on your data, you just need to subset based on character values and not numerical as pointed out by @camille:

ggplot(df, aes(x = week, y = result, 
                 color =year,
                 fill = year))+
    geom_col(data = subset(df, year == "2020")) +
    geom_line(data = subset(df, year != "2020"))

enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you for the quick response! The code that you provided works with my full data set I figured out the issue that I was having pertains more so to the structure of my data more so than ggplot. Basically, I was filtering my data frame prior to running the ggplot code. When I was subsetting my data based on the criteria, it was looking off the original data frame, pre filters. This was adding more rows of data and causing error when the visual was produced. It seems obvious now that I think about it! – mf17 Feb 20 '20 at 02:01
  • You're welcome, I edited my answer to provide an alternative way based on the data you provided – dc37 Feb 20 '20 at 02:02