简体   繁体   English

如何制作带有 x 轴值不按顺序的值的 R 图

[英]How to make an R plot with values on x-axis values not in order

I have the following example dataframe and want to plot columns b (x-axis) and d (y-axis) but based on column a .我有以下示例数据框,想要绘制列b (x 轴)和d (y 轴),但基于列a Meaning I want the rows in b and d that correspond to value 1 in column a to be plotted next to each other (as points or vertical lines) and then similarly for value 2 in column a etc. (all in one graph).含义我想在bd,在列中的对应于值1被彼此相邻地绘制的行(如点或垂直线),然后类似地在列中的等值2(都在同一个图)。

I am having trouble doing this as it would mean the values on the x-axis will increase and decrease/fluctuate.我在这样做时遇到了麻烦,因为这意味着 x 轴上的值会增加和减少/波动。

a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)

df <- data.frame("a" = a, "b" = b, "d" = d)

plot(df$b, df$d)

I have tried basic plotting as shown above (not the results I want) and have tried other methods to relabel the values on the x-axis but that is also incorrect.我已经尝试了如上所示的基本绘图(不是我想要的结果)并尝试了其他方法来重新标记 x 轴上的值,但这也是不正确的。

Lastly, I am unable to use any R libraries as I cannot download them on the computer I am using.最后,我无法使用任何 R 库,因为我无法在我使用的计算机上下载它们。

Thank you in advance for any help!预先感谢您的任何帮助!

Not sure if I understand the question properly, but perhaps plotting the interaction of "a" and "b" would work?不确定我是否正确理解了这个问题,但也许绘制“a”和“b”的交互会起作用?

a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)

df <- data.frame("a" = a, "b" = interaction(b, a), "d" = d)
df$b <- factor(df$b, levels = df$b)

png("test.png", width = 900, height = 400, units = "px")
plot(df$b, df$d)
dev.off()

示例_2.png

Edit编辑

Or maybe remove the x-axis labels and add them in 'manually'?或者也许删除 x 轴标签并将它们添加到“手动”中?

a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)

df <- data.frame("a" = a, "b" = b, "d" = d)

png("test.png", width = 900, height = 400, units = "px")
plot(seq_along(df$b), df$d, xaxt = "none")
axis(1, at = seq_along(df$b), labels = df$b)
dev.off()

示例_3.png

Edit 2编辑 2

Another approach that may better "preserve the spacing" of the x-axis is to split the dataframe and plot each group separately:另一种可能更好地“保留 x 轴间距”的方法是拆分数据框并分别绘制每个组:

a <- c(1,1,1,1,1,1,1, 2,2,2,2,2,2,2, 3,3,3,3,3,3,3)
b <- c(1,4,5,7,8,10,13, 5,7,10,11,14,17,23, 3,7,11,16,19,26,29)
d <- c(.4,.15,.76,.07,.18,.11,.12, .23,.45,.25,.11,.16,.2,.5, .48,.9,.13,.75,.4,.98,.3)

df <- data.frame("a" = a, "b" = b, "d" = d)

split_df <- split(df, df$a)

par(mfrow = c(1, 3), mar = c(2, 2, 1, 1))
plot(split_df$`1`$b, split_df$`1`$d, ylim = c(0, 1))
plot(split_df$`2`$b, split_df$`2`$d, ylim = c(0, 1))
plot(split_df$`3`$b, split_df$`3`$d, ylim = c(0, 1))

Created on 2021-10-15 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 10 月 15 日创建

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

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