[英]How to change the scale of x-y axis of graph usinig R?
I made a graph using R and this is the code.我用 R 做了一个图表,这是代码。 In this graph, I wanna change the scale of xy axis.
在此图中,我想更改 xy 轴的比例。
For x-axis, I want the scale unit becomes 1, not 20. Also, For y-axis, I want the scale unit becomes 100, not 500 as the graph currently has.对于 x 轴,我希望比例单位变为 1,而不是 20。此外,对于 y 轴,我希望比例单位变为 100,而不是当前图形中的 500。
What codes do I need to add more in this code?我需要在此代码中添加哪些代码? Could you tell me the method?
能告诉我方法吗?
Many thanks.非常感谢。
enter image description here在此处输入图片说明
hist(m2$GW, xlim=c(0,100), ylim=c(0,2000), main="GW distribution (Cultivar 1)", xlab="Grain weight (mg)", ylab="Frequency (%)", col="white", las=1)
Welcome to Stack Overflow J. Kim.欢迎来到 Stack Overflow J. Kim。
You can scale your x-axis simply multiplying m2$GW
by the new scale and diving it by the old scale.您可以缩放 x 轴,只需将
m2$GW
乘以新比例并按旧比例进行划分。 Following your example, if you want the x-axis to become 1, instead of 20, you simply do: m2$GW*(20/1)
.按照您的示例,如果您希望 x 轴变为 1,而不是 20,您只需执行:
m2$GW*(20/1)
。 To plot the histogram with a scaled x-axis you can do:要使用缩放的 x 轴绘制直方图,您可以执行以下操作:
hist(m2$GW, xlim=c(0,100), ylim=c(0,2000), main="GW distribution (Cultivar 1)",
xlab="Grain weight (mg)", ylab="Frequency (%)", col="white", las=1)
Since a histogram counts the frequency of a variable, you shouldn't change the y-axis scale, otherwise, you would lose the main information of the plot, which is the count of each x-axis' value.由于直方图计算变量的频率,因此不应更改 y 轴比例,否则会丢失绘图的主要信息,即每个 x 轴值的计数。
If you still want to scale the y-axis, you can use ggplot2 package.如果你仍然想缩放 y 轴,你可以使用ggplot2包。 Something like this may solve your problem:
这样的事情可能会解决您的问题:
library(ggplot2)
# define convenient scales for x and y
x_scale = 500/100
y_scale = 20/1
# plot
ggplot(m2) +
geom_histogram(aes(x = GW*x_scale, y = stat(count)*y_scale),
fill = 'white', colour = 'black') +
labs(title = 'GW distribution (Cultivar 1)',
x = 'Grain weight (mg)', y = 'Frequency (%)') +
theme(plot.title = element_text(hjust = 0.5))
I hope it helped you somehow.我希望它以某种方式帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.