user13203033
user13203033

Reputation: 133

How to make line chart in R with the Sum of a Value, by Period, separated by Group?

I am trying to use GG Plot to turn the following data (there are more periods than what is listed here - allowing for a line chart)

obs District    ZoneID  Period  SUM_activity
1   Northwestern    Northern: 53A   2019-02-06 - 2019-03-06 4
2   Northwestern    Northern: 53B   2019-02-06 - 2019-03-06 0
3   Northwestern    Northwestern: 61A   2019-02-06 - 2019-03-06 88
4   Northwestern    Northwestern: 61B   2019-02-06 - 2019-03-06 44
5   Northwestern    Northwestern: 61D   2019-02-06 - 2019-03-06 212
6   Northwestern    Northwestern: 62A   2019-02-06 - 2019-03-06 38
7   Northwestern    Northwestern: 62B   2019-02-06 - 2019-03-06 18
8   Northwestern    Northwestern: 62C   2019-02-06 - 2019-03-06 65
9   Northwestern    Northwestern: 62D   2019-02-06 - 2019-03-06 4
10  Northwestern    Northwestern: 63A   2019-02-06 - 2019-03-06 107
11  Northwestern    Northwestern: 63B   2019-02-06 - 2019-03-06 19
12  Northwestern    Northwestern: 63C   2019-02-06 - 2019-03-06 56
13  Northwestern    Northwestern: 63D   2019-02-06 - 2019-03-06 165
14  Northwestern    Northwestern: DATA  2019-02-06 - 2019-03-06 28
15  Northwestern    Northwestern: DATB  2019-02-06 - 2019-03-06 26
16  Northwestern    Northwestern: DATC  2019-02-06 - 2019-03-06 114
17  Northwestern    Outside Zones 2019-02-06 - 2019-03-06   1501
18  Southern    Outside Zones 2019-02-06 - 2019-03-06   2062
19  Southwestern Outside Zones  2019-02-06 - 2019-03-06 1351

Into a chart that looks like this:

chart

Basically - Summing up the sum_Activity variable. However the line must be specific to the District value.

I am just confused about how to structure the code (this is for a bar chart - I can't get the line chart to work - but this is the name of my dataframe/vars:

by_species <- pro_ag %>%
  filter(ZoneID!= 'Outside Zones' & District = 'Northwestern') %>%
  group_by(Period) %>%
  summarize(sumPro=sum(SUM_activity))
ggplot(by_species, aes(x=Period,
                         y=sumPro)) +
  geom_col() 

Do I have to use by_species? Is there an easy way to add options (so that it is colored and appears similar to how I have in the image), also are there options for smoothing of the line?

Thanks!!!

Upvotes: 0

Views: 650

Answers (1)

dc37
dc37

Reputation: 16178

Are you looking for something like that ?

pro_ag %>%
  filter(ZoneID!= 'Outside Zones' & District = 'Northwestern') %>%
  group_by(Period, District) %>%
  summarize(sumPro=sum(SUM_activity)) %>%
  ggplot(aes(x=Period,y=sumPro, group = 1)) +
  geom_smooth(se = FALSE) 

Here, I made a fake example:

df <- data.frame(District = rep(LETTERS[1:3],each = 100),
                 Period = rep(rep(letters[1:10], each = 10),3),
                 Value = sample(1:100,300, replace = TRUE))

And I calculate the sum of values per District and Period before plotting it in ggplot2:

library(dplyr)
library(ggplot2)

df %>% group_by(District, Period) %>%
  summarise(Sum = sum(Value)) %>%
  ggplot(aes(x = Period, y = Sum, group = District, color = District))+
  #geom_point()+
  #geom_line()+
  geom_smooth(se = FALSE)

enter image description here

Does it answer your question ?

Upvotes: 1

Related Questions