[英]fitting a diagonal line to graph in ggplot
I have a data frame that looks like this我有一个看起来像这样的数据框
> 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
I melt this data to我将这些数据融化为
> 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 lines in total) (共48行)
> class(m_wider_data$x)
[1] "factor"
> class(m_wider_data$value)
[1] "integer"
Then I plot 2 graphs (corresponding to the 2 levels of 'treat') using然后我绘制 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
However, the diagonal line ends up wrong然而,对角线最终错了
I want it to go through (0,0) (1,1) (2,2) (3,3) (4,4) (5,5).我希望它通过 (0,0) (1,1) (2,2) (3,3) (4,4) (5,5)。
In addition, I want cut the margins so that the graph area start so close to x=0 and y=0 as possible.此外,我想削减边距,以便图形区域开始尽可能接近 x=0 和 y=0。 I have tried to change x to a numeric variable and use
我试图将 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))
But it is not working.但它不起作用。
Is there a way to achieve this?有没有办法实现这一目标? Thank you in advance!
先感谢您!
Maybe this is what you are looking for:也许这就是你要找的:
It's best to convert numeric.最好转换成数字。 However, when converting a factor to a numeric you have to keep in mind that you first have to convert to a character.
但是,在将因子转换为数字时,您必须记住首先必须转换为字符。 Otherwise your first category "0" becomes 1, your second category "1" becomes 2 and so on.
否则,您的第一个类别“0”变为 1,您的第二个类别“1”变为 2,依此类推。 Therefore always use
as.numeric(as.character(x))
.因此始终使用
as.numeric(as.character(x))
。
After this change your code should work fine.在此更改后,您的代码应该可以正常工作。 Nonetheless I made some small changes.
尽管如此,我还是做了一些小改动。 I set the limits via coord_cartesain.
我通过 coord_cartesain 设置了限制。 And also I used the expand argument to
scale_x/y_continuous
to set the default expansion (= 5 percent) to 0. BTW: You don't need to add an extra ".05" to you limits as ggplot will expand your axes by default.而且我还使用了
scale_x/y_continuous
的 expand 参数将默认扩展(= 5%)设置为 0。顺便说一句:您不需要为限制添加额外的“.05”,因为 ggplot 默认会扩展您的轴. (If you are fine with the default expansion you ould simply remove scale_x/y_continuous
or if you want a different expansion try eg c(0.025, 0)
which will expand the axis by 2.5 percent on both sides.) (如果您对默认扩展没问题,您只需删除
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
DATA数据
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)
I think your problem is a result of x being a factor and not a number.我认为你的问题是 x 是一个因素而不是一个数字的结果。 It is best to perform that conversion early in the process.
最好在该过程的早期执行该转换。
Also, I have updated the matrix transformation using the more modern pivot_longer
instead of melt
另外,我使用更现代的
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.