Reputation: 394
I have data.frame as follows
test=data.frame(start=rep("0",10),end=rep("100",10),Typ1=c("530","630","500","400","350","600","1032","378","430","567"),Type2=c("100","70","50","120","130","50","75","86","90","95"),Type3=c("10","50","40","22","13","45","15","36","19","20"))
>test
start end Type1 Type2 Type3
0 100 530 100 10
0 100 630 70 50
0 100 500 50 40
0 100 400 120 22
0 100 350 130 13
0 100 600 50 45
0 100 1032 75 15
0 100 378 86 36
0 100 430 90 19
0 100 567 95 20
All I want is to plot the above data frame with x-axis denoting the start and end and Y-axis denoting the Type1, Type2 and Type3. I tried the following code but it throwed me error
ggplot(test,aes(x=c(start,end)),y=c(Type1,Type2,Type3)) +geom_density()
Kindly guide me. Thanks in advance.
Upvotes: 0
Views: 4274
Reputation: 27792
First, cast your data to long format (works better for ggplot), then plot
I also created some x-values...
library(data.table)
library(ggplot2)
plotdata <- setDT(test)[, x := seq(0,100,length.out = 10)]
plotdata <- melt( setDT(test), id.vars = c("x"), measure.vars = patterns("^Typ"), value.factor = FALSE )
ggplot( data = plotdata,
aes( x = value,
color = variable,
fill = variable)
) +
geom_density()
Upvotes: 1
Reputation: 1119
Your data frame is arranged well, you do not need to specify the start and end in the data frame.
ggplot expect that each row in your df is an observation and each column is a variable. That means we need to reshape the data
library(tidyverse)
test <- data.frame(start=rep("0",10), end=rep("100",10),
Type1=c("530","630","500","400","350","600","1032","378","430","567"),
Type2=c("100","70","50","120","130","50","75","86","90","95"),
Type3=c("10","50","40","22","13","45","15","36","19","20"))
Notice I fixed a typo. We now can transform it. You can read more about it here (tidy).
df <- gather(test, key = 'Type', value = 'Value', contains('type'))
The contains indicates which variables to use to fill the value column. key and value are just to indicate which names to give the columns.
> head(df)
start end Type Value
1 0 100 Type1 530
2 0 100 Type1 630
3 0 100 Type1 500
4 0 100 Type1 400
5 0 100 Type1 350
6 0 100 Type1 600
We can now easily plot it using ggplot.
ggplot(df, aes(x = Value, group = Type, fill = Type, color = Type)) +
geom_density(alpha = 0.3)
Upvotes: 3
Reputation: 527
I'm not sure if I understand your questions correctly, but I assume that you want a density plot of the three variables Type1, Type2, Type3 and the values indicate the hights of the line. Then you could do something like:
test = data.frame(x = seq(0, 100, length.out = 10),
Type1 = c(530, 630, 500, 400, 350, 600, 1032, 378, 430, 567),
Type2 = c(100, 70, 50, 120, 130, 50, 75, 86, 90, 95),
Type3 = c(10, 50, 40, 22, 13, 45, 15, 36, 19, 20))
ggplot(test, aes(x = x)) +
geom_line(aes(y = Type1, color = "Type 1")) +
geom_line(aes(y = Type2, color = "Type 2")) +
geom_line(aes(y = Type3, color = "Type 3"))
What you have to do is use an own layer for every column of your data.frame.
Upvotes: 3