r - how to plot x and y based on several conditions? -


i have dataframe, constructed follows:

col1<-c(0.10, 0.21, 0.34, 0.39, 0.54, 0.67,0.89) col2<-c (0,100,500,1000,3000,5000,8000 ) col3<-c ("b", "a","a","c", "c","b",  "c" ) mydata<-data.frame(col1,col2,col3)      > mydata       col1   col2 col3  #   1 0.10    0    b  #   2 0.21   100    #   3 0.34   500    #   4 0.39  1000   c  #   5 0.54  3000   c  #   6 0.67  5000   b  #   7 0.89  8000   c 

here code create plot between col2 , col1:

plot(mydata$col2,mydata$col1,      main = "plot of data",      xlab = "x", ylab= "y",      pch =c(21,21,21)[mydata$col3], bg=c("blue","grey", "red")[mydata$col3],  ) legend("topright",pch=c(21,21,21), title="categories:",        c("a","b", "c"),pt.bg=c("blue","red","grey")) 

it creates plot:

enter image description here

i have problems in codes:

1, there should 7 points in plot, 1 has been covered topright box. there better method points display?

2, want have different scales in x-axis label tick marks desired interval, plot 2. see interval between 100 , 500 same 3000 , 8000, smaller values not suppressed extremely left side, want. how achieve that?

3, seems in syntax pch =c(21,21,21)[mydata$col3], bg=c("blue","grey", "red")[mydata$col3], haven't specified colour maps "a", "b" or "c". if want respectively map colours "blue","grey", "red" "a", "b" , "c" given in col3. how specify it?

any advice appreciated.

for second one, use customize axis. in case, want customize x axis. in plot function, need add xaxt="n", disable original x axis. use axis add yours. in case, try

axis(1, at=c(a,b,c,d,e),labels=c(a,b,c,d,e),xxx,xxx) 

where a, b,c,d,e values want have.

example:

y <- x <- c(1:5) plot(x, y,type="l", xaxt="n") axis(1, at=c(1,1.3,1.5,1.7,2.8),labels=c(1,1.3,1.5,1.7,2.8)) 

enter image description here

for third question, how adding column of mapping colors dataframe? following: instance,

 mydata$color <-ifelse(mydata$col3=="a","blue",ifelse(mydata$col3=="b","red","grey")) 

which maps color blue, b color red , c grey. , in plot,

plot(mydata$col2,mydata$col1,xxxxxx, bg=mydata$color,  ) 

enter image description here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -