Reputation: 389
I want to make a plot in R from the following data:
1.txt 3.103 12
2.txt 3.020 11
3.txt 2.929 10
4.txt 3.020 11
5.txt 2.929 10
a.txt 4.254 39
b.txt 4.228 38
c.txt 4.175 36
d.txt 4.175 36
e.txt 4.175 36
I want to have a variable which stores the values from the for the second column and one for the third. The only problem is that they are too lines are too far apart and if I set ylim=c(3,40)
, the first line is almost straight which is not true in my case.
How can I solve this?
I was thinking maybe of splitting the plot with a horizontal line and below it to have the right values for the second column and above it the ones for the third column, only I do not know how to do this. Is this a good approach?
This is the code I have so far:
plot(a,type="o",col="blue",pch=0,lty=2,xaxt="n",ylim=c(3,40))
lines(b,type="o",col="green",pch=5,lty=3)
axis(side=1,at=seq(1,45,1),cex.axis=0.5,las=1)
and this is the plot:
Upvotes: 1
Views: 352
Reputation: 56054
You could normalise before plotting:
#reproducible data
df <- read.table(text="1.txt 3.103 12
2.txt 3.020 11
3.txt 2.929 10
4.txt 3.020 11
5.txt 2.929 10
a.txt 4.254 39
b.txt 4.228 38
c.txt 4.175 36
d.txt 4.175 36
e.txt 4.175 36")
#normalise 0-1 range
#(m - min(m))/(max(m)-min(m))
df$V2 <- (df$V2-min(df$V2))/(max(df$V2)-min(df$V2))
df$V3 <- (df$V3-min(df$V3))/(max(df$V3)-min(df$V3))
#plot
plot(df$V2,type="o",col="blue",pch=0,lty=2,xaxt="n")
lines(df$V3,type="o",col="green",pch=5,lty=3)
axis(side=1,at=seq(1,45,1),cex.axis=0.5,las=1)
Upvotes: 2
Reputation: 59345
Here's a solution using facets with ggplot
. df
is defined as in the other answer.
colnames(df) <- c("id","y1","y2")
df$x <- 1:nrow(df)
library(reshape2) # for melt(...)
library(ggplot2)
gg <- melt(df,id="x", measure.vars=c("y1","y2"))
ggplot(gg, aes(x=x,y=value,color=variable)) +
geom_point(shape=1, size=4)+
geom_line(linetype="dashed")+
facet_grid(variable~., scales="free_y")
This has the (perhaps minor) advantage of showing the actual values for y1 and y2.
Upvotes: 3