简体   繁体   English

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

[英]plot multiple circular angle occurrences using ggplot2 in r

I am trying to make a circular plot of several angles' occurrences in one graph similar to these ones: 我正在尝试在一张与这些图相似的图中绘制多个角度的圆形图: 在此处输入图片说明

The idea is to represent the distribution of each torsional angle (alpha, beta, etc.) with one circle. 这个想法是用一个圆来表示每个扭转角(α,β等)的分布。 The higher the occurrence of that angle the darker the line within that circle. 该角度出现的次数越高,该圆内的线越暗。

My input file looks like this: 我的输入文件如下所示:

  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

First column is just index number and columns 2-12 are the distributions of 12 angles I want to plot. 第一列只是索引号,而2-12列是我要绘制的12个角度的分布。 My angle values go from -180:180. 我的角度值从-180:180开始。 I can easily change my input data depending on what I need for r. 我可以根据需要r轻松更改输入数据。 I am new to r and trying to do this using ggplot2. 我是r的新手,并尝试使用ggplot2执行此操作。 My main problem is I am not sure what is the best way to represent the distribution data in this case. 我的主要问题是我不确定在这种情况下代表分布数据的最佳方法是什么。 One way that I thought of is to make 12 circles by ylim(c(1,12)) and represent each angle distribution by a rectangle with min and max distribution values as coordinates for that rectangle (so first column (or first angle) will be represented by a rectangle with ymin=1 and ymax=2, xmin=min(of column 1) and xmax=max(of column 1), etc.): 我想到的一种方法是用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()

This way I just tried to do one angle (column) to see if it will work, but it did not. 这样,我只是尝试做一个角度(列)以查看它是否可以工作,但没有成功。 I have also tried using geom_point or geom_boxplot (which are better to represent distribution data than geom_rect ) but was unsuccessful. 我也尝试使用geom_pointgeom_boxplot (比geom_rect更好地表示分布数据),但没有成功。

Any insights, ideas, comments are greatly appreciated! 任何见解,想法,评论都将不胜感激!

One possibility may be to use geom_segment . 一种可能是使用geom_segment First you need to melt your data to a ggplot -able long format. 首先,你需要melt你的数据到ggplot -able长格式。 Let the angle values represent x and xend values. 让角度值代表xxend值。 Then create y and yend values which put the different types of angles on separate circles. 然后创建yyend值,将不同类型的角度放在单独的圆上。 Here's something to begin with: 从以下内容开始:

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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