0

I created a histogram in R. At the moment, the numbers 1 - 8 are written on the x-axis (for each number a bar). I would like to change the numbers into the wind direction, e.g. instead of 1, there should stand "west". I tried:

scale_x_discrete(labels=c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))

But is not working.I also tried:

scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"), labels=c("North", "North East", "East", "South East", "South", "South West", "West", "North West"))

Here is my script:

options(stringsAsFactors = FALSE)

input1 <- "C:\\Users\\wind_direction.csv"

wind_direction <- read.csv(input1, sep=";")
library(ggplot2)

p3 <- ggplot(wind_direction, aes(x=winddirection)) + 
  geom_bar(color="black", fill="grey", width=0.9)+ 
  theme_bw() +
  scale_y_continuous(limits = c(0, 55), breaks = seq(0,55,10),expand=c(0,0)) +
  scale_x_discrete(labels = c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))

print(p3)

Here is a sample of my data:

head(wind_direction)
         day     time winddirection
1 31.07.2018 12:51:57             3
2 31.07.2018 12:55:16             3
3 31.07.2018 12:56:29             3
4 31.07.2018 13:25:05             3
5 31.07.2018 13:36:54             3
6 31.07.2018 13:55:37             3

My histogram looks at the moment like this

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Dolphin94
  • 319
  • 1
  • 2
  • 8
  • Can you post sample data in `dput` format? Please edit **the question** with the output of `dput(wind_direction)`. Or, if it is too big with the output of `dput(head(wind_direction, 20))`. – Rui Barradas Dec 27 '18 at 12:58
  • Have you tried to replace `aes(x=winddirection)` with `aes(x=as.character(winddirection))` ? – Valentin_Ștefan Dec 27 '18 at 13:05
  • See if [this question](https://stackoverflow.com/questions/5096538/customize-axis-labels) can help. – Rui Barradas Dec 27 '18 at 13:09

1 Answers1

1

I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)).

So, you just need to be sure that winddirection is character or factor when you map it into x.

Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete you declare that 1 is north.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
  • Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x". – Dolphin94 Dec 27 '18 at 13:23
  • Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.frame `wind_direction` (?) I can replicate your error if instead of `winddirection` I use `wind_direction` in `aes(x=as.character(winddirection))` - might that be the case? – Valentin_Ștefan Dec 27 '18 at 13:29
  • Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction)) – Dolphin94 Dec 27 '18 at 13:40
  • That is confusing because you cannot map your data frame `wind_direction` into `x`, but your column `winddirection` . Did you swap their names somehow? – Valentin_Ștefan Dec 27 '18 at 13:44
  • I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working. – Dolphin94 Dec 27 '18 at 14:14