【问题标题】:plot multiple circular angle occurrences using ggplot2 in r在 r 中使用 ggplot2 绘制多个圆角出现
【发布时间】:2014-06-28 11:45:20
【问题描述】:

我正在尝试在与这些类​​似的图表中绘制多个角度出现的圆形图:

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

我的输入文件如下所示:

  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 更能代表分布数据)但没有成功。

非常感谢任何见解、想法、cmets!

【问题讨论】:

  • 这很有趣,但您能否将您的数据提供给dput()

标签: r ggplot2 plot histogram


【解决方案1】:

一种可能是使用geom_segment。首先,您需要将melt 您的数据转换为ggplot-able 长格式。让角度值代表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()

【讨论】:

  • 非常感谢@Henrik,这对我很有用。
  • @user3290846 很高兴您发现我的回答很有用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 2022-08-13
  • 2017-09-12
  • 1970-01-01
  • 2021-02-10
  • 2017-06-19
相关资源
最近更新 更多