简体   繁体   English

两个刻度在同一轴上

[英]Two scales in the same axis

I'm not sure if it's possible do what I want to. 我不确定是否有可能做我想做的事。 I want to draw an x axis with two scales like the picture below. 我想用两个比例绘制一个x轴,如下图所示。 Thanks a lot, 非常感谢,

在此输入图像描述

ggplot2 version can look like this: ggplot2版本看起来像这样:

library(ggplot2)

x = c(1,2,3,4,5, 10,20,30,40,50)
y = c(1,2,2,3,4, 2,1,3,5,5)
# You should introduce cond - condition to separate axises - by yourself
df = data.frame(x=x,y=y,cond=ifelse(x>5,"x2","x1"))

ggplot(df, aes(x,y,group=cond)) + geom_line() + geom_point(aes(shape=cond), size=4) + facet_grid(.~cond, scales="free_x")

Which produces this plot: 这产生了这个情节: 在此输入图像描述

Here is a version using base R graphics. 这是一个使用基础R图形的版本。 You can probably play with it a bit more to get things just how you want it, but this is basically what you are after. 您可以更多地使用它来获得您想要的东西,但这基本上就是您所追求的。

plot.new()
par(mfcol=c(1,2))
plot(1:5,ann=FALSE,bty="n",type="l",ylim=c(0,25))
grid(ny=NA)
par(mar=c(5.1, 0, 4.1, 2.1))
plot(c(10,20,30,40,50),c(8,5,15,20,20),yaxt="n",ann=FALSE,bty="n",type="l",ylim=c(0,25))
grid(ny=NA)
par(new=TRUE)
par(mfcol=c(1,1))
par(bty="l")
par(mar=c(5.1, 4.1, 4.1, 2.1))
plot(NA,ylim=c(0,25),type="n",xaxt="n",yaxt="n",ann=FALSE)
box()
grid(nx=NA,ny=NULL)

在此输入图像描述

edit- snazzed it up a bit 编辑 - 把它搞砸了一下

I'm not altogether sure, but as a starting point, take a look at ?facet_grid() in ggplot2 . 我并不完全确定,但作为一个起点,看一看?facet_grid()ggplot2

Getting the two plots side by side should not be too hard. 并排获得两个地块不应该太难。 Then you would probably want to turn off the axis and then add back the appropriate scales 然后你可能想要关闭轴然后添加适当的比例

Ask google about axis.line = theme_blank() 问谷歌关于axis.line = theme_blank()

Good luck! 祝好运!

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

相关问题 在R中的同一图和同一轴上绘制两个比例不同的数据集 - Plot two datasets with different scales on the same graph, same axis in R ggplot2-如何使用主要和次要y轴在同一图上对具有不同比例的两个变量进行箱图绘制? - ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis? facet_grid中同一轴上的离散和连续比例 - discrete and continuous scales on same axis in facet_grid 如何在具有两个 y 轴的 ggplot 中移动极限刻度 - How to shift limit scales in a ggplot with two y axis Plot 3 个变量和两个 y 轴(不同刻度)的折线图 - Plot a line chart with 3 variables and two y axis (different scales) 一个R图上的两个x轴刻度 - Two x-axis scales on one R plot R, ggplot - 图形共享相同的 y 轴但具有不同的 x 轴比例 - R, ggplot - Graphs sharing the same y-axis but with different x-axis scales 绘制图表两行,两个比例,两个y轴以及底部带有R和图的额外数据 - Plot Chart Two lines, two scales, two y-axis and extra data at bottom with R and plot ggplot2,将两个比例应用于同一个情节? 自上而下的barplot - ggplot2, applying two scales to the same plot? Top down barplot 在 r 中的同一图表上绘制具有不同 x 轴百分比比例的 geom_col 和 geom_line - Plot geom_col and geom_line with different x-axis percentage scales on the same graph in r
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM