绘制数据中变量的“范围" [英] Plot 'ranges' of variable in data
问题描述
我有范围形式的观察结果例如:A 13-20、B 15-30、C 23-40、D 2-11我想以起始值和结束值的形式在 R 中绘制它们,例如.A 的 13 和 20(上限和下限,如果您可以说),以便可视化并找出某些观察组合的共同范围.在 R 中有没有一种快速的方法来做到这一点?我认为这是我遇到的一个非常微不足道的问题,但我现在想不出怎么做.
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.
推荐答案
这是一个使用 ggplot
的解决方案.根本不清楚您的数据采用什么格式,因此这里假设数据框包含 id
(AD)、min
和 max
列.
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()
我添加了很多自定义功能,只是为了让您了解它是如何完成的.您使用 aes(...)
函数来告诉 ggplot
df
中的哪些列映射到图形的各种美感.因此,例如 aes(x=id)
告诉 ggplot
x 轴的值将在 id
列中找到代码>df等
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.
编辑:对 OP 评论的回应.
EDIT: Response to OP's comment.
要更改轴文本的大小,请使用 theme(...)
函数,如下所示:
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))
这里我把 x 轴的文字放大了.玩转 size=...
以达到您想要的方式.另请阅读文档 (?theme
) 以获取其他格式选项的列表.
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.
这篇关于绘制数据中变量的“范围"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!