使用r中的ggplot2绘制多个圆角出现

时间:2014-05-10 21:35:23

标签: r ggplot2 plot histogram

我试图在一个类似于这些图形的图形中制作几个角度出现的圆形图: enter image description here

这个想法是用一个圆圈表示每个扭转角度(α,β等)的分布。该角度的出现越高,该圆圈内的线条越暗。

我的输入文件如下所示:

  1.00   14.01  171.64  -17.49  168.69 -150.94   10.27  -20.86  145.12  145.05   -7.43 -161.90   -5.87
  2.00   18.15 -172.52   -7.12  162.23  164.93   11.60   -1.73  154.66  158.51  -27.71 -174.80    0.62
  3.00    4.94 -167.07   -3.86  144.74 -164.88   -2.33  -19.91  145.94  148.27   -5.93  175.08  -12.85
  4.00  -15.02 -150.01  -12.18  155.77 -143.32    2.34  -12.78  137.45  142.44  -18.65  165.76   14.60
  5.00  -11.59 -154.16   -3.87  145.04 -170.26   11.28   -2.69  152.88  162.17  -28.51 -168.32   -9.84

第一列只是索引编号,第2-12列是我想要绘制的12个角度的分布。我的角度值从-180:180。我可以根据r所需的内容轻松更改输入数据。 我是r的新手,并试图使用ggplot2来做到这一点。我的主要问题是我不确定在这种情况下表示分发数据的最佳方式是什么。我想到的一种方法是通过ylim(c(1,12))制作12个圆圈,并用矩形表示每个角度分布,最小和最大分布值作为该矩形的坐标(因此第一列(或第一个角度)将用ymin = 1和ymax = 2,xmin = min(第1列)和xmax = max(第1列)等矩形表示:)

data = read.table("myinputfile")
ggplot(data, aes(xvar=-180:180,y=data$V2)) +
  ylim(c(1,13)) +
  geom_rect(aes(ymin=1, ymax=2, xmin=min(data$V2), xmax=max(data$V2))) +
  coord_polar()

这样我只是尝试做一个角度(列)以查看它是否可行,但事实并非如此。 我还尝试过使用geom_pointgeom_boxplot(这比表示geom_rect更好地表示分发数据)但是没有成功。

非常感谢任何见解,想法和评论!

1 个答案:

答案 0 :(得分:2)

一种可能性是使用geom_segment。首先,您需要melt将您的数据改为ggplot - 长格式。设角度值代表xxend值。然后创建yyend值,将不同类型的角度放在不同的圆上。这是开始的事情:

library(reshape2)
library(ggplot2)

df2 <- melt(df, id.var = "V1")

df2$xend <- df2$value
df2$y <- as.numeric(as.factor(df2$variable))
df2$yend <- df2$y + 1

ggplot(data = df2, aes(x = value, xend = xend, y = y, yend = yend)) +
  geom_segment() +
  coord_polar() +
  scale_x_continuous(breaks = seq(-180, 180, 45)) +
  scale_y_continuous(breaks = min(df2$y):max(df2$yend)) +
  theme_bw()

enter image description here