-1

I have been working on a clustered bar plot but my x and y axis scales are off. I would like to y axis to go from 0-30 and the data to be arranged according to month from May-October. I have been stuck on this for quite a while. I have included all of the code that I have tried.

str(Plaster_2019_Data)

dat.g <- gather(Plaster_2019_Data, type, value, -Date)

ggplot(dat.g, aes(Date, value)) + 
  geom_bar(aes(fill = Date), stat = 'identity', position = "dodge2") +
  facet_grid(.~type) +
  expand_limits(y = c(1,30))


ggplot(dat.g, aes(Date,value)) + 
  geom_bar(aes(fill = type), stat = 'identity', position = "dodge2") +
    expand_limits(y = c(1,30))

Data

Date    Surface pH  Temperature

5/13/2019   12.08   8.56    11.16
5/29/2019   11.68   8.90    8.76
6/10/2019   8.69    9.07    14.65
6/24/2019   2.26    7.49    17.51
7/8/2019    4.54    7.77    23.82
8/5/2019    2.13    8.17    25.29
8/19/2019   6.34    8.62    26.50
9/4/2019    9.33    9.03    24.31
9/16/2019   10.98   8.58    21.02
9/30/2019   9.59    8.61    17.33
10/14/2019  16.07   8.70    10.39
11/14/2019  9.12    8.07    6.38
dc37
  • 15,840
  • 4
  • 15
  • 32
  • 1
    What did not work for you here: https://stackoverflow.com/a/59654173/8583393 ? It is best to share data using `dput`, i.e. post the outcome of `dput(head(dat.g, 12))` at the end of your question. – markus Jan 09 '20 at 22:04
  • The x axis values are still out of order and the y axis values are also out of order and seem to be placed randomly. I tried all of the troubleshooting from yesterday but I am stuck in the same spot. – Samantha McKinney Jan 09 '20 at 22:08
  • 1
    To me the issue seems related to the types of some of your columns (e.g. your date column is not of type date). If you post `dput(head(dat.g, 12))` you'll have an answer within minutes. – markus Jan 09 '20 at 22:14

1 Answers1

1

Are you looking for something like that ?

library(tidyverse)
df$Date <- as.Date(df$Date, format = "%m/%d/%Y")
  dat.g <- gather(df, type, value, -Date)

ggplot(dat.g, aes(Date, value)) + 
  geom_bar(aes(fill = type), stat = 'identity', position = "dodge2") +
  facet_grid(.~type) +
  expand_limits(y = c(1,30))+
  scale_x_date(date_breaks = "month",
               date_labels = "%b")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

Data

structure(list(Date = structure(c(3L, 4L, 5L, 6L, 7L, 9L, 8L, 
12L, 10L, 11L, 1L, 2L), .Label = c("10/14/2019", "11/14/2019", 
"5/13/2019", "5/29/2019", "6/10/2019", "6/24/2019", "7/8/2019", 
"8/19/2019", "8/5/2019", "9/16/2019", "9/30/2019", "9/4/2019"
), class = "factor"), Surface = c(12.08, 11.68, 8.69, 2.26, 4.54, 
2.13, 6.34, 9.33, 10.98, 9.59, 16.07, 9.12), pH = c(8.56, 8.9, 
9.07, 7.49, 7.77, 8.17, 8.62, 9.03, 8.58, 8.61, 8.7, 8.07), Temperature = c(11.16, 
8.76, 14.65, 17.51, 23.82, 25.29, 26.5, 24.31, 21.02, 17.33, 
10.39, 6.38)), class = "data.frame", row.names = c(NA, -12L))
dc37
  • 15,840
  • 4
  • 15
  • 32
  • Thank you so much! I am getting this error when I run the code through: Error: Invalid input: date_trans works with objects of class Date only. Do you know how to fix it? – Samantha McKinney Jan 09 '20 at 22:11
  • I have edited my answer below, you have to transform your column Date in a `date` format using: `df$Date <- as.Date(df$Date, format = "%m/%d/%Y")`, then you can use `scale_x_date` in your plot. (change `df` according to the name of your dataset, here `Plaster_2019_Data`) – dc37 Jan 09 '20 at 22:14