简体   繁体   中英

How to reformat a conditional density plot in R?

The following reproducible example creates a conditional density plot depicting change by aspect. I've included NoData in the example because my original dataset also has NoData.

aspect = c("N", "N", "E", "S", "SW", "SW", "E", "W", "N", "N")
change = c(-1, NA, 1, NA, 1, 1, -1, 1, -1, NA)
mydata = data.frame(aspect,change)

x = factor(mydata$change)

cdplot(x~mydata$aspect)

在此处输入图片说明

Related Questions:

  • How do I replace the numerical values on the X-axis with the associated text values (eg N, SW, etc)?
  • Is it possible to have control over the order of text values on the X-axis? I believe, by default, the values on the X-axis correspond to the alphabetically ordered text values in "aspect".
  • How do I replace "NA" on the X-axis with "No Change"?

just to show what @Roland said in the comments, you use the function in a wrong manner.

aspect = c("N", "N", "E", "S", "SW", "SW", "E", "W", "N", "N")
change = sample(rep(c(-1,1,NA), each = 100),100,replace=T)
mydata = data.frame(aspect,change,stringsAsFactors=T)
str(mydata)
'data.frame':   100 obs. of  2 variables:
 $aspect: Factor
$change: num 
h <- cdplot(aspect~change, data = mydata)

在此处输入图片说明

and now h contains the conditional density functions over the levels of aspect.

str(h)
List of 4
 $ E :function (v)  
 $ N :function (v)  
 $ S :function (v)  
 $ SW:function (v)  

eg

h$E(-Inf)
[1] 0.21875
h$E(Inf)
[1] 0.25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM