简体   繁体   English

R 中的星图

[英]Star Graph in R

I want to draw a star graph in R with dates and nb axis.我想用日期和 nb 轴在 R 中绘制星图。 The centre of the date is 02.07.2018.日期的中心是 02.07.2018。

Sample image:示例图片:

在此处输入图像描述

My data:我的数据:

dates   NB
3.01.2018   -80
15.01.2018  -75
8.02.2018   70
20.02.2018  65
4.03.2018   45
28.03.2018  20
9.04.2018   55
21.04.2018  60
3.05.2018   10
15.05.2018  40
8.06.2018   80
20.06.2018  50
02.07.2018 0
14.07.2018  -110
7.08.2018   50
19.08.2018  100
12.09.2018  45
24.09.2018  -20
6.10.2018   5
30.10.2018  20
11.11.2018  30
23.11.2018  -40
5.12.2018   -50
17.12.2018  -60

Where can I start?我可以从哪里开始? I don't have any idea.我没有任何想法。 Thank you.谢谢你。

ggplot graph ggplot 图

This is the equivalent with ggplot :这与ggplot等效:

library(ggplot2)
center <- subset(dd, dates=="2018-07-02")
ggplot(dd, aes(dates, NB, xend = center$dates, yend = center$NB)) +
  geom_segment(color="blue") +
  geom_point()

The graph will look like this:该图将如下所示:

在此处输入图像描述

Edit编辑

If you want to include all dates to the x axis, you can use this code:如果要将所有日期包含在x轴上,可以使用以下代码:

library(ggplot2)
center <- subset(dd, dates=="2018-07-02")
ggplot(dd, aes(dates, NB, xend = center$dates, yend = center$NB)) +
  geom_segment(color="blue") +
  geom_point() +
  theme(axis.text.x = element_text(angle = 60, hjust = 1)) +
  scale_x_date(breaks = dd$dates)

Hope this helps.希望这可以帮助。

First, make sure to read in your data and convert the date column to a proper date object in R.首先,确保读取您的数据并将日期列转换为 R 中的正确日期 object。

dd <- read.table(text="
dates   NB
3.01.2018   -80
15.01.2018  -75
8.02.2018   70
20.02.2018  65
4.03.2018   45
28.03.2018  20
9.04.2018   55
21.04.2018  60
3.05.2018   10
15.05.2018  40
8.06.2018   80
20.06.2018  50
02.07.2018 0
14.07.2018  -110
7.08.2018   50
19.08.2018  100
12.09.2018  45
24.09.2018  -20
6.10.2018   5
30.10.2018  20
11.11.2018  30
23.11.2018  -40
5.12.2018   -50
17.12.2018  -60", header=T, stringsAsFactors=FALSE)
dd$dates <- as.Date(dd$dates, "%d.%m.%Y")

Here I just used read.table and then used as.Date to convert the first column to proper date values.在这里,我只使用了read.table ,然后使用as.Date将第一列转换为正确的日期值。 This makes it easy to start the plot.这使得启动 plot 变得容易。 For example例如

plot(NB~dates, dd)

Then to add all the lines, we can easily add a bunch of segments with a common end point.然后添加所有的线,我们可以很容易地添加一堆具有共同端点的线段。 Here we grab your reference point, and then draw the segments.在这里,我们抓住您的参考点,然后绘制线段。

plot(NB~dates, dd)
center <- subset(dd, dates=="2018-07-02")
segments(dd$dates, dd$NB, center$dates, center$NB)

Technically this is drawing the segments on top of the points.从技术上讲,这是在点的顶部绘制线段。 If you want to switch the order and make things blue, you can do如果你想切换顺序并使事情变蓝,你可以做

center <- subset(dd, dates=="2018-07-02")
plot(NB~dates, dd, type="n")
segments(dd$dates, dd$NB, center$dates, center$NB, col="blue")
points(dd$dates, dd$NB, pch=20)

在此处输入图像描述

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

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