Home > Software engineering >  How to plot some elements of dataframe in R
How to plot some elements of dataframe in R

Time:12-02

I have a some data frames, and for example, 8th dframe is dfQ[[8]] :

c(16, 14, 15, 14, 14, 12, 13, 14, 13, 12, 13, 12, 12, 13, 13, 
13, 13, 13, 14, 14, 16, 13, 12, 13, 12, 13, 12, 13, 12, 12, 12, 
12, 12, 13, 13, 13, 13, 13, 14, 17, 17, 17, 18, 11, 12, 14, 13, 
13, 12, 11, 11, 11, 9, 9, 9, 11, 12, 12, 9, 13, 14, 14, 16, 12, 
15, 14, 14, 14, 14, 11, 13, 13, 14, 13, 13, 12, 11, 11, 11, 9, 
11, 11, 10, 9, 8, 8, 8, 8, 6, 7, 8, 9, 9, 11, 12, 9, 12, 11, 
11, 9, 12, 12, 12, 13, 9, 14, 9, 12, 13, 9, 12, 12, 13, 13, 9, 
13, 14, 14, 13, 12, 13, 12, 12, 12, 11, 12, 9, 11, 8, 9, 9, 8, 
8, 1)

His length is 134. And I don't understand how to plot the elements in range 124:134 for this data frame?

I try the same code implement, but I get error differences for xy cord

st <- c(length(dfQ[8]])-10:length(dfQ[8]]))

plot(st, dfQ[[8]],
     type= "o",pch=16,lwd=0.4, lty=3, xlab = 'Steps', ylab = '',cex = 0.6,
col = red)
abline(h=c(0.000450), lwd=1.5, lty=1, col="black")
mtext("MAX F A.U.", col = "black", adj=-0.1, padj= 0.1, cex=0.8)
grid(nx = NULL, ny = NULL,lty = 2, col = "gray", lwd = 1)

CodePudding user response:

You can subset your values using [] and specify the index inside the brackets.

plot(dfQ[[8]][124:134])

If you want to index using length, then use this (you were quite close):

plot(dfQ[[8]][(length(dfQ[[8]])-10):length(dfQ[[8]])])

And if you want to do it over multiple data frames, you can use lengths.

dfQ = list(rnorm(100), rnorm(110), rnorm(166))
lengths = lengths(dfQ)
plot(dfQ[[2]][(lengths[2]-10):lengths[2]])

CodePudding user response:

There are several ways

 rm(list = ls())

data<-c(16, 14, 15, 14, 14, 12, 13, 14, 13, 12, 13, 12, 12, 13, 13, 
          13, 13, 13, 14, 14, 16, 13, 12, 13, 12, 13, 12, 13, 12, 12, 12, 
          12, 12, 13, 13, 13, 13, 13, 14, 17, 17, 17, 18, 11, 12, 14, 13, 
          13, 12, 11, 11, 11, 9, 9, 9, 11, 12, 12, 9, 13, 14, 14, 16, 12, 
          15, 14, 14, 14, 14, 11, 13, 13, 14, 13, 13, 12, 11, 11, 11, 9, 
          11, 11, 10, 9, 8, 8, 8, 8, 6, 7, 8, 9, 9, 11, 12, 9, 12, 11, 
          11, 9, 12, 12, 12, 13, 9, 14, 9, 12, 13, 9, 12, 12, 13, 13, 9, 
          13, 14, 14, 13, 12, 13, 12, 12, 12, 11, 12, 9, 11, 8, 9, 9, 8, 
          8, 1)

data<-as.data.frame(data)

df_124_134=data.frame(data[124:134,])

#data.124.134...
#1               12
#2               11
#3               12
#4                9
#5               11
#6                8
#7                9
#8                9
#9                8
#10               8
#11               1



plot(df_124_134, yaxt="n", ylab="", 
     xlab="Values between 124 and 134", 
     pch=16,
     col="darkblue", size=30)
axis(2,cex.axis=1.2)

enter image description here

  •  Tags:  
  • r
  • Related