Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何在ggplot2的单个图形中绘制一个X变量和两个Y变量?_R_Ggplot2 - Fatal编程技术网

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

R 如何在ggplot2的单个图形中绘制一个X变量和两个Y变量?,r,ggplot2,R,Ggplot2,以下是我到目前为止所做的 现在,我如何在图表顶部添加时间 另外,想知道如何使用文本上的R代码行而不是作为文件附加。请更明确地指定您需要的内容,我假设您只需要给定数据的点。 此外,当您发布示例数据时,请以文本而不是图像的形式发布,以便我们可以复制它 使用ggplot2库进行精细打印。我使用了tidyverse中的tibble而不是dataframe,但您可以使用其中任何一种 # Make your data frame but different months data.cars <- t

以下是我到目前为止所做的

现在,我如何在图表顶部添加时间


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

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

使用ggplot2库进行精细打印。我使用了tidyverse中的tibble而不是dataframe,但您可以使用其中任何一种

# 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"))
#将您的数据框设置为不同的月份
数据车