假设您的数据称为 df:
df = structure(list(jan = c(0L, 0L, 0L, 0L), feb = c(0L, 0L, 0L, 0L
), mar = c(0L, 0L, 0L, 0L), apr = c(0L, 0L, 0L, 0L), may = c(0L,
0L, 0L, 0L), jun = c(0L, 1L, 0L, 0L), jul = c(0L, 1L, 1L, 1L),
aug = c(1L, 1L, 1L, 1L), sep = c(0L, 1L, 1L, 1L), oct = c(0L,
0L, 1L, 1L), nov = c(0L, 0L, 0L, 0L), dec = c(0L, 0L, 0L,
0L), someValue = c(109.24673, 108.24444, 247.25433, 192.22873
)), class = "data.frame", row.names = c(NA, -4L))
你可以在基础 R 中做到这一点(过去的美好时光):
ind = which(df[,1:12]==1,arr.ind=TRUE)
row col
[1,] 2 6
[2,] 2 7
[3,] 3 7
[4,] 4 7
[5,] 1 8
[6,] 2 8
[7,] 3 8
[8,] 4 8
[9,] 2 9
[10,] 3 9
[11,] 4 9
[12,] 3 10
[13,] 4 10
所以你要绘制的是 x 上的列号和 someValue 对应的行值,让我们把它放到 data.frame 中
plotdf = data.frame(x=ind[,"col"],
y=df$someValue[ind[,"row"]])
x y
1 6 108.2444
2 7 108.2444
3 7 247.2543
4 7 192.2287
5 8 109.2467
6 8 108.2444
7 8 247.2543
8 8 192.2287
9 9 108.2444
10 9 247.2543
11 9 192.2287
12 10 247.2543
13 10 192.2287
你可以只使用基础 R(过去的美好时光):
plot(plotdf,xlim=c(1,12),xaxt="n",ylab="somevalue")
months = colnames(df)[1:12]
axis(1,at=1:12,labels=months)
或者,如果您喜欢一些花哨的图形,我们可以使用 plotly,使用我们之前定义的 plotdf 和月份:
library(plotly)
plot_ly(x=plotdf$x,y=plotdf$y,type="scatter") %>%
layout(xaxis=list(range=c(0,13),tickvals=1:12,
dtick=1,ticktext = months))