简体   繁体   English

如何在ggplot中用相同的x绘制不同的y?

[英]How to plot different y with the same x in ggplot?

Let's say I have a dataframe with one column x and other variables y1, y2, ... all continuos.假设我有一个数据框,其中包含一列 x 和其他变量 y1, y2, ... 全部连续。

What's the quickest way to plot x ~ y1 and y2 on two different graphs but like they were in facet_wrap?在两个不同的图上绘制 x ~ y1 和 y2 的最快方法是什么,但就像它们在 facet_wrap 中一样?

I know I can build multiple ggplots and use grid.arrange (but this way I am pasting the same code over and over for each y with no changes but the index of y) but is it possible to do it with facets?我知道我可以构建多个 ggplots 并使用 grid.arrange (但这样我会一遍又一遍地为每个 y 粘贴相同的代码,除了 y 的索引外没有任何变化)但是是否可以使用 facet 来做到这一点?

Seems rather simple but I am having trouble with facets.看起来很简单,但我在方面遇到了麻烦。

This type of problems generaly has to do with reshaping the data.这类问题通常与重塑数据有关。 The format should be the long format and the data is in wide format.格式应该是长格式,数据是宽格式。
I will use the first 3 columns of built in dataset iris as an example dataset.我将使用内置数据集iris的前 3 列作为示例数据集。

library(ggplot2)

df1 <- iris[1:3]
names(df1) <- c("x", "y1", "y2")

df1_long <- reshape2::melt(df1, id.vars = "x")
head(df1_long)

ggplot(df1_long, aes(x, value, colour = variable)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_grid(rows = vars(variable))

在此处输入图片说明

暂无
暂无

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

相关问题 如何 plot 相同 x 的两个 Y 值与 ggplot 散点图 plot - How to plot two Y values for the same x with ggplot for a scatter plot Plot ggplot2 中 y 变量相同但 x 变量不同的两个箱线图 - Plot two box plots with same y variable but different x variables in ggplot2 如何在ggplot中绘制两个不同的y值? - How to plot two different y value in ggplot? ggplot使用purrr map()绘制具有多个y的相同x - ggplot using purrr map() to plot same x with multiple y's ggplot2 中的相关图,x 和 y 轴具有不同的变量 - correlation plot in ggplot2 with different variables in x and y axis 循环图 ggplot y for x 用于线性回归的不同类别,如何依次 plot 类别? - Loop graphs ggplot y for x for different categories with linear regression, How to consequetively plot categories? ggplot2-如何使用主要和次要y轴在同一图上对具有不同比例的两个变量进行箱图绘制? - ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis? 如何使用ggplot将两个不同比例的y轴放在绘图的同一侧? - How to put two y-axis with different scale on the same side of the plot with ggplot? ggplot2如何显示具有相同y但不同x的两条不同回归线 - How ggplot2 shows two different regression lines with same y but different x 我如何在同一个 plot 上使用不同的 x 和 y plot 两条回归线? - How do I plot two regression lines on the same plot with different x and y?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM