如何根据规模为两个完全不同的变量构建单个图?

时间:2016-04-06 17:16:02

标签: r graph ggplot2 lattice

我有这个数据集

data of Staff strength and total Applications received

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))

我想进行探索性分析,并希望根据收到的申请来比较员工人数是否在增长。我使用excel绘制了一个折线图: enter image description here

这不是很有意义。 我还记录了两个几乎都得到了预期结果的变量,但我想知道带有日志的图表是否可以解释为非数学家。因为我想在演示文稿中使用这些图表给我的管理人员,他们对统计数据或数学知之甚少。 我的问题是如何处理这种情况以绘制有意义的图表。 我有一种直觉,认为R可能有更好的解决方案(这就是我在这里问的原因)而不是Excel,但问题是'如何'?

任何帮助都将受到高度赞赏。

3 个答案:

答案 0 :(得分:3)

一项建议是将您的指标更改为某种类型的比率指标。例如,progressBar.progress。在下文中,我将使用staff per applications

staff per 1,000 applications

Plot 01

我们可以在不使用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") 的情况下获得相同的结果:

ggplot2

Base R Plot

或者,您可以使数据集更加整洁(有关详细信息,请参阅thisthis和/或this)并使用{{1}绘制两个方面} scale:

with(df, 
      plot(x = year, y = staff / (applications / 1000), type = "l", main = "Staff per 1,000 Applications") + 
        points(x = year, y = staff / (applications / 1000), pch = 21, cex = 2, bg = "black")
     )

Plot 02

答案 1 :(得分:1)

我建议您将facet_gridscales = "free_y"一起使用。

ggplot(reshape2::melt(df, 1), aes(year, value)) + 
    geom_line() + geom_point() + 
    facet_grid(variable ~ ., scales = 'free_y')

您将获得的输出是, Output

答案 2 :(得分:0)

enter image description here

    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)