Reputation: 29
I was trying to make a stacked line plot with following data (referred as Test.xls):
Test <- read.table(text = "
Date A B C D
1002EST 0.225164194 0.089942967 0.684289684 0.000603155
1007EST 0.606697971 0.375931286 0.015410986 0.001959757
1012EST 0.745430542 0.240069576 0.01186502 0.002634862
1017EST 0.586552052 0.253384789 0.159091468 0.000971692
1022EST 0.719846808 0.085252303 0.193054704 0.001846186
1106EST 0.908801738 0.075007121 0.015196251 0.00099489
1111EST 0.901317416 0.083624883 0.011020257 0.004037444
1116EST 0.838439325 0.116000974 0.042570946 0.002988756
1121EST 0.822848944 0.14935052 0.024948372 0.002852164
1126EST 0.878149037 0.107215826 0.011199368 0.003435768
1206EST 0.806550999 0.127171158 0.062039302 0.00423854",
header = TRUE, stringsAsFactors = FALSE)
I use the "import dataset" option from RStudio to import the data. And R plotting code is like below:
library(reshape2)
Test_melt <- melt(Test, "Date")
library(ggplot2)
ggplot(Test_melt, aes(x = Date, y = value, fill = variable)) +
geom_area(position = 'stack')
But I found my script did not generate any plots, which like below: Rplot
How could I solve this problem?
Upvotes: 1
Views: 1152
Reputation: 6264
I offer a tidy
approach to remove the melt
step.
library(ggplot2)
library(tidyr)
Test %>%
gather(variable, value, -Date) %>%
ggplot(aes(x = Date, y = value)) +
geom_area(aes(fill = variable, group = variable))
Upvotes: 2
Reputation: 33772
Try adding group = variable
, and there is no need for position = "stack"
:
ggplot(Test_melt, aes(Date, value)) + geom_area(aes(fill = variable, group = variable))
Upvotes: 4