'Changing x-axis labels in r
ive got ggplot (geombar), with names of the months on x-axis.i have some data from different days. But i would like to have the names on the axis corresponding to the specific dates.
Date. Percent. Category
2020-02-03. 30. Apple
2020-02-03. 25. Banana
2020-02-03. 15. Citron
2020-02-03. 30. Orange
2020-02-07. 40. Apple
2020-02-07. 20. Orange
2020-02-07. 40. Banana
2020-04-12. 65. Apple
2020-04-12. 35. Banana
Ive got 80 dates from one year
ggplot(data, aes(fill=Category, y=percents, x=date)) +
geom_bar(position="fill", stat="identity")
Solution 1:[1]
I assumed this would be a duplicate but I was not able to find this question on stackoverflow, so here is a potential solution:
library(tidyverse)
df <- read.table(text = "Date Percent Category
2020-02-03 30 Apple
2020-02-03 25 Banana
2020-02-03 15 Citron
2020-02-03 30 Orange
2020-02-07 40 Apple
2020-02-07 20 Orange
2020-02-07 40 Banana
2020-04-12 65 Apple
2020-04-12 35 Banana", header = TRUE)
df$Date <- as.Date(df$Date)
ggplot(df, aes(fill=Category, y=Percent, x=Date)) +
geom_bar(position="fill", stat="identity") +
scale_x_date(labels = unique(df$Date),
breaks = unique(df$Date)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2022-04-01 by the reprex package (v2.0.1)
Edit
This excellent answer by @teunbrand provides another approach to your problem: https://stackoverflow.com/a/68746906/12957340
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
