Plot 'ranges' of variable in data

余生颓废 提交于 2019-12-21 19:14:32

问题


I have observations in the form of ranges For eg: A 13-20, B 15-30, C 23-40, D 2-11 I want to plot them in R in form of the starting value and the end value for eg. 13 and 20 for A(upper and lower limits if you may say) in order to visualize and find out what ranges are common to certain combinations of observations. Is there a quick way to do this in R ? I think this is a very trivial problem I am having but I cant think of anyway to do it right now.


回答1:


Here is a solution using ggplot. It's not clear at all what format your data is in, so this assumes a data frame with columns id (A-D), min, and max.

df <- data.frame(id=LETTERS[1:4], min=c(13,15,23,2), max=c(20,30,40,11))
library(ggplot2)
ggplot(df, aes(x=id))+
  geom_linerange(aes(ymin=min,ymax=max),linetype=2,color="blue")+
  geom_point(aes(y=min),size=3,color="red")+
  geom_point(aes(y=max),size=3,color="red")+
  theme_bw()

I've added a lot of customization just to give you an idea of how it's done. You use the aes(...) function to tell ggplot which columns in df map to various aesthetics of the graph. So for instance aes(x=id) tells ggplot that the values for the x-axis are to be found in the id column of df, etc.


EDIT: Response to OP's comment.

To change the size of axis text, use the theme(...) function, as in:

ggplot(df, aes(x=id))+
  geom_linerange(aes(ymin=min,ymax=max),linetype=2,color="blue")+
  geom_point(aes(y=min),size=3,color="red")+
  geom_point(aes(y=max),size=3,color="red")+
  theme_bw()+
  theme(axis.text.x=element_text(size=15))

Here I made the x-axis text bigger. Play around with size=... to get it the way you want. Also read the documentation (?theme) for a list of other formatting options.




回答2:


It is not clear whether the dataset has range column as string or not i.e. '13-20', '15-30' etc. or if it is two numeric columns as showed in the created example.

matplot(m1, xaxt='n', pch=1, ylab='range')
axis(1, at=seq_len(nrow(m1)), labels=row.names(m1))
s1 <- seq_len(nrow(m1))
arrows(s1, m1[,1], s1, m1[,2], angle=90, length=0.1)

If the data has string column (d1)

library(splitstackshape)
d2 <- setDF(cSplit(d1, 'range', '-'))
matplot(d2[,-1], xaxt='n', pch=1, ylab='range')
axis(1, at=seq_len(nrow(d2)), labels=d2$Col1)
arrows(s1, d2[,2], s1, d2[,3], angle=90, length=0.1)

data

m1 <- matrix(c(13,20, 15,30, 23,40, 2,11), 
  byrow=TRUE,dimnames=list(LETTERS[1:4],NULL), ncol=2)

d1 <- data.frame(Col1=LETTERS[1:4], 
      range=c('13-20', '15-30', '23-40', '2-11'), stringsAsFactors=FALSE)


来源:https://stackoverflow.com/questions/27205629/plot-ranges-of-variable-in-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!