繁体   English   中英

如何在ggplot2的单个图中绘制一个X变量和两个Y变量?

[英]How to plot one X variable with two Y variables in a single graph in ggplot2?

到目前为止,这是我所做的。

在此处输入图片说明

现在如何在图表上方添加时间?

另外,想知道如何在文本上使用R代码行而不是附加为文件。

请更明确地指定您需要的内容,我将假设您只需要给定数据即可。 另外,当您发布示例数据时,请以文本形式而不是图像形式发布它,以便我们进行复制。

使用ggplot2库进行漂亮的绘图。 我使用了tidyverse的小标题而不是数据框,但是您可以使用其中任何一个。

# Make your data frame but different months
data.cars <- tibble(Cars = c("Toyota", "Honda", "Nissan"), Month = c("Jan", 
"Feb", "Mar"), `Distance(km)` = c(124,221,154), `Time(min)`= c(21,34,23))

# Make the Months column a factor instead of strings (Jan=1, Feb=2, etc)
data.cars$Month <- as.factor(data.cars$Month)

# Make a plot with a common x-axis - the month and different y-axes
ggplot(data.cars, aes(x = Month)) +
  geom_point(aes(y = `Distance(km)`, color = "blue")) +
  geom_point(aes(y = `Time(min)`, color = "red"))

暂无
暂无

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

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