Reputation: 53
I have the following data which I am trying to plot as a stacked area chart:
week Wildtype ARE
3 3770 3740
4 3910 3920
5 3660 3640
6 3750 3790
7 3940 3930
8 3940 3940
9 3830 3810
10 3710 3720
11 3730 3720
12 357 358
Using this code for a stacked area chart
library(reshape2)
library(ggplot2)
rm(list=ls())
df <- read.csv("Mo_data/mo_qpcr_data2.csv", comment.char = "#", sep=",")
df_melt <- melt(df, id=c("week"))
p1 <- ggplot() + geom_area(aes(y = value, x = week, fill = variable), data = df_melt)
p1
I get the plot that I want but it isn't quite right.
How do I change the plot so that the x-axis displays each week in the time series rather than just 5.0, 7.5 and 10.0?
Upvotes: 0
Views: 580
Reputation: 23
Ggplot is treating your Week column as a number (rightly so, because it is a number), so your x axis appears as continuous. If you want to treat weeks as discrete values you can:
1) Change the week column to characters or factors
df_melt$week <- as.factor(df_melt$week)
df_melt$week <- as.factor(df_melt$week)
2) Tell the x axis where you want the breaks with scale_axis_discrete
more info here
Upvotes: 0
Reputation: 182
I would add this to the code
+ scale_x_continuous(breaks= unique(df$week) )
library(reshape2)
library(ggplot2)
rm(list=ls())
df <- read.csv("Mo_data/mo_qpcr_data2.csv", comment.char = "#", sep=",")
df_melt <- melt(df, id=c("week"))
p1 <- ggplot() + geom_area(aes(y = value, x = week, fill = variable), data = df_melt) + scale_x_continuous(breaks= unique(df$week) )
p1
Upvotes: 1