R 如何根据比例为两个完全不同的变量构造一个单独的图?
我有这个数据集R 如何根据比例为两个完全不同的变量构造一个单独的图?,r,graph,ggplot2,lattice,R,Graph,Ggplot2,Lattice,我有这个数据集 df一个建议是将您的度量值更改为某种类型的比率度量。例如,每个应用程序的员工人数。在下文中,我将使用每1000个应用程序中的员工数: library(ggplot2) df <- data.frame(year = seq(1970, 2015, by = 5), staff = c(219, 231, 259, 352, 448, 427, 556, 555, 602, 622), applicati
df一个建议是将您的度量值更改为某种类型的比率度量。例如,每个应用程序的员工人数
。在下文中,我将使用每1000个应用程序中的员工数
:
library(ggplot2)
df <- data.frame(year = seq(1970, 2015, by = 5),
staff = c(219, 231, 259, 352, 448, 427, 556, 555, 602, 622),
applications = c(5820, 7107, 6135, 16119, 19381, 36611, 54962, 45759, 40358, 458582))
ggplot(data = df, aes(x = year, y = staff / (applications / 1000))) +
geom_point(size = 3) +
geom_line() +
ggtitle("Staff per 1,000 Applications")
或者,您可以使数据集更加整洁(请参阅和/或了解更多信息),并使用free_y
scales将其绘制为两个方面:
library(tidyr)
df_tidy <- gather(df, measure, value, -year)
ggplot(data = df_tidy, aes(x = year, y = value)) +
geom_point(size = 3) +
geom_line() +
facet_grid(measure ~ ., scales = "free_y")
library(tidyr)
df_tidy一个建议是将您的度量值更改为某种类型的比率度量。例如,每个应用程序的员工人数
。在下文中,我将使用每1000个应用程序中的员工数
:
library(ggplot2)
df <- data.frame(year = seq(1970, 2015, by = 5),
staff = c(219, 231, 259, 352, 448, 427, 556, 555, 602, 622),
applications = c(5820, 7107, 6135, 16119, 19381, 36611, 54962, 45759, 40358, 458582))
ggplot(data = df, aes(x = year, y = staff / (applications / 1000))) +
geom_point(size = 3) +
geom_line() +
ggtitle("Staff per 1,000 Applications")
或者,您可以使数据集更加整洁(请参阅和/或了解更多信息),并使用free_y
scales将其绘制为两个方面:
library(tidyr)
df_tidy <- gather(df, measure, value, -year)
ggplot(data = df_tidy, aes(x = year, y = value)) +
geom_point(size = 3) +
geom_line() +
facet_grid(measure ~ ., scales = "free_y")
library(tidyr)
df_整洁
我们可以使用这个过程:
图书馆(GG2)
图书馆(E2)
ggplot(df,aes(年))+
geom_线(aes(y=staff,color=“staff”)+
geom_线(aes(y=应用,color=“应用”))
df
我们可以使用这个过程:
图书馆(GG2)
图书馆(E2)
ggplot(df,aes(年))+
geom_线(aes(y=staff,color=“staff”)+
geom_线(aes(y=应用,color=“应用”))
df我建议您使用facet\u grid
和scales=“free\u y”
您将得到的输出是,
我建议您使用facet\u grid
和scales=“free\u y”
您将得到的输出是,
在ggplot2
中,您可以免费使用刻面。您确定total application中的最后一个数字不是错误或输入错误吗?在绘图中,可以为“Santed”放置备用y轴,以使其不被压缩。您可以计算相关性。@fishtank。。这不是一个错误,因为2011年采用的在线应用系统导致了大量的应用程序。在ggplot2
中,您可以免费使用刻面。您确定总应用程序中的最后一个数字不是错误或打字错误吗?在绘图中,可以为“Santed”放置备用y轴,以使其不被压缩。您可以计算相关性。@fishtank。。这并不是一个错误,因为2011年采用的在线应用系统大大增加了应用程序的数量。
we can use this process:
library(ggplot2)
library(reshape2)
ggplot(df, aes(year)) +
geom_line(aes(y = staff, colour = "staff")) +
geom_line(aes(y = applications, colour = "applications"))
df <- data.frame(year = seq(1970, 2015, by = 5),
staff = c(219, 231, 259, 352, 448, 427, 556, 555, 602, 622),
applications = c(5820, 7107, 6135, 16119, 19381, 36611, 54962, 45759, 40358, 458582)
ggplot(reshape2::melt(df, 1), aes(year, value)) +
geom_line() + geom_point() +
facet_grid(variable ~ ., scales = 'free_y')