繁体   English   中英

在ggplot中拟合对角线以绘制图形

[英]fitting a diagonal line to graph in ggplot

我有一个看起来像这样的数据框

> wider_data
# A tibble: 12 x 6
   treat    FR    CC    HP Other x    
   <fct> <int> <int> <int> <int> <fct>
 1 1         0     2     5     3 0    
 2 1         2     1     5     1 1    
 3 1         3     5     3     2 2    
 4 1         0     2     4     1 3    
 5 1         1     2     4     1 4    
 6 1         0     2     4     0 5    
 7 2         4     1     4     2 0    
 8 2         1     5     2     0 1    
 9 2         4     0     1     4 2    
10 2         0     5     2     3 3    
11 2         0     3     4     1 4    
12 2         1     5     1     2 5  

我将这些数据融化为

> m_wider_data <- melt(wider_data) #Using treat, levels, x as id variables
Using treat, x as id variables
> m_wider_data
   treat x variable value
1      1 0       FR     0
2      1 1       FR     2
3      1 2       FR     3
4      1 3       FR     0
5      1 4       FR     1
6      1 5       FR     0
7      2 0       FR     4
8      2 1       FR     1
9      2 2       FR     4
10     2 3       FR     0
11     2 4       FR     0
12     2 5       FR     1
13     1 0       CC     2
14     1 1       CC     1
15     1 2       CC     5
16     1 3       CC     2

(共48行)

> class(m_wider_data$x)
[1] "factor"
> class(m_wider_data$value)
[1] "integer"

然后我绘制 2 个图(对应于 2 个级别的“治疗”)使用

plot_test <- m_wider_data %>%
  ggplot(aes(x = x, y = value, colour = variable, group = variable)) +
  facet_wrap(vars(treat), ncol = 2) +
  geom_point() +
  geom_line(aes(linetype=variable)) +
  geom_abline(slope = 1, intercept = 0) +
  labs(x = "X", y = "Y") +
  ggtitle('Table') +
  theme(legend.title=element_blank()) #turns of the legend title

然而,对角线最终错了

图表

我希望它通过 (0,0) (1,1) (2,2) (3,3) (4,4) (5,5)。
此外,我想削减边距,以便图形区域开始尽可能接近 x=0 和 y=0。 我试图将 x 更改为数字变量并使用

plot_test + 
  scale_x_continuous(breaks=c(0,1,2,3,4,5),limits = c(-0.05, 5.05)) +
  scale_y_continuous(breaks=c(0,1,2,3,4,5),limits = c(-0.05, 5.05))

但它不起作用。

有没有办法实现这一目标? 先感谢您!

也许这就是你要找的:

最好转换成数字。 但是,在将因子转换为数字时,您必须记住首先必须转换为字符。 否则,您的第一个类别“0”变为 1,您的第二个类别“1”变为 2,依此类推。 因此始终使用as.numeric(as.character(x))

在此更改后,您的代码应该可以正常工作。 尽管如此,我还是做了一些小改动。 我通过 coord_cartesain 设置了限制。 而且我还使用了scale_x/y_continuous的 expand 参数将默认扩展(= 5%)设置为 0。顺便说一句:您不需要为限制添加额外的“.05”,因为 ggplot 默认会扩展您的轴. (如果您对默认扩展没问题,您只需删除scale_x/y_continuous或者如果您想要不同的扩展,请尝试例如c(0.025, 0)这将使轴在两侧扩展 2.5%。)

library(ggplot2)
library(dpylr)

plot_test <- m_wider_data %>%
  ggplot(aes(x = as.numeric(as.character(x)), y = value, colour = variable, group = variable)) +
  facet_wrap(vars(treat), ncol = 2) +
  geom_point() +
  geom_line(aes(linetype=variable)) +
  geom_abline(slope = 1, intercept = 0) +
  labs(x = "X", y = "Y") +
  ggtitle('Table') +
  theme(legend.title=element_blank()) +
  coord_cartesian(xlim = c(0,5), ylim = c(0,5)) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))
plot_test

数据

m_wider_data <- read.table(text = "treat x variable value
1      1 0       FR     0
2      1 1       FR     2
3      1 2       FR     3
4      1 3       FR     0
5      1 4       FR     1
6      1 5       FR     0
7      2 0       FR     4
8      2 1       FR     1
9      2 2       FR     4
10     2 3       FR     0
11     2 4       FR     0
12     2 5       FR     1
13     1 0       CC     2
14     1 1       CC     1
15     1 2       CC     5
16     1 3       CC     2", header = TRUE)

m_wider_data$x <- factor(m_wider_data$x)

我认为你的问题是 x 是一个因素而不是一个数字的结果。 最好在该过程的早期执行该转换。
另外,我使用更现代的pivot_longer而不是melt更新了矩阵变换

widedf<-read.table(header = TRUE, text="   treat    FR    CC    HP Other x    
1         0     2     5     3 0    
1         2     1     5     1 1    
1         3     5     3     2 2    
1         0     2     4     1 3    
1         1     2     4     1 4    
1         0     2     4     0 5    
2         4     1     4     2 0    
2         1     5     2     0 1    
2         4     0     1     4 2    
2         0     5     2     3 3    
2         0     3     4     1 4    
2         1     5     1     2 5")


library(tidyr)
#make the table long
df <- pivot_longer(widedf, -c("treat", "x"), names_to="variable", values_to="value")
#convert x from a character to an integer
df$x <- as.integer(as.character(df$x))


library(ggplot2)
plot_test <- df %>%
   ggplot(aes(x = x, y = value, colour = variable, group = variable)) +
   facet_wrap(vars(treat), ncol = 2) +
   geom_point() +
   geom_line(aes(linetype=variable)) +
   geom_abline(slope = 1, intercept = 0) +
   labs(x = "X", y = "Y") +
   ggtitle('Table') +
   theme(legend.title=element_blank()) #turns of the legend title
plot_test

暂无
暂无

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

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