【问题标题】:second y-axis with other scale [duplicate]具有其他比例的第二个 y 轴 [重复]
【发布时间】:2018-10-19 06:08:09
【问题描述】:

我用 ggplot2 geom_bar 创建了一个条形图,并希望在条形图中有两个指标。我用融化来做到这一点。但是,我现在需要另一个刻度的第二个 y 轴,因为这两个指标的数字相差太大。

下面是我使用的数据框和代码:

df <- data.frame(categories = c("politics", "local", "economy", "cultural events", 
                                "politics", "local", "economy", "cultural events"), 
               metric = c("page", "page", "page", "page", 
                          "product", "product", "product", "product"), 
               value = c(100L, 50L, 20L, 19L, 
                         950000L, 470000L, 50000L, 1320L))

下面是我用来创建绘图和第二个 y 轴的代码:

x <- ggplot(df, aes(x=categories, y=value, fill = metric))
x + geom_bar(stat = "identity", position = "dodge") +
  scale_y_continuous(sec.axis=sec_axis(~. *1000), limits=c(1,1000))

但是,现在图表中不再出现条形了...有人知道如何解决这个问题吗?

【问题讨论】:

  • 可重现的示例数据?
  • 您应该考虑对变量进行分面,而不是添加转换后的第二个 y 轴,这不会显示您希望它们出现的 page-data,因为第二个轴只是重新标记轴并且不使用page 数据创建新的绘图层。您的绘图中没有条形,因为您有limits = c(1, 1000)。将其更改为limits = c(0, 1000)
  • 请使用dput 添加数据,这样我们就可以在自己的会话中运行它,而不是粘贴表格。作为第一个猜测,尝试从aes 内部删除df$。当您引用 aes 中的变量时,您永远不需要使用 df$
  • @kath 感谢您对限制的编辑和建议。你是对的,现在我有页面数据。但是,面对变量是什么意思?我以前从来没有这样做过......
  • FWIW...死双轴情节,死! (这里有一篇关于为什么他们显然很烂的博客文章blog.datawrapper.de/dualaxis

标签: r ggplot2 scale geom-bar yaxis


【解决方案1】:

另一种方法是使用highcharter

library(dplyr)
library(tidyr)
library(highcharter)

#convert your data in wide format
df <- df %>% spread(metric, value)

#plot
highchart() %>% 
  hc_xAxis(categories = df$categories) %>%
  hc_yAxis_multiples(
    list(lineWidth = 3, title = list(text = "Page")),
    list(opposite = TRUE, title = list(text = "Product"))
  ) %>% 
  hc_add_series(type = "column", data = df$page) %>% 
  hc_add_series(type = "line", data = df$product, yAxis=1) # replace "line" with "column" to have it in bar format

输出图为:

样本数据:

df <- structure(list(categories = structure(c(4L, 3L, 2L, 1L, 4L, 3L, 
2L, 1L), .Label = c("cultural events", "economy", "local", "politics"
), class = "factor"), metric = structure(c(1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L), .Label = c("page", "product"), class = "factor"), 
    value = c(100L, 50L, 20L, 19L, 950000L, 470000L, 50000L, 
    1320L)), .Names = c("categories", "metric", "value"), row.names = c(NA, 
-8L), class = "data.frame")

【讨论】:

  • 这太棒了,正是我想要的 :) 非常感谢!
  • 很高兴它有帮助:)
【解决方案2】:

您可以将它们堆叠在一起(或刻面),而不是在同一个图上显示不同的条:

正如@ConorNeilson 在 cmets 中已经提到的,您不必(也不应该)使用df$ 指定变量。 ggplot 知道在指定的 data.frame df 中查找它们。
facet_grid-call 中的 free_y 参数可以在 y 轴上显示不同的比例。

library(ggplot2)
ggplot(df, aes(x = categories, y = value, fill = metric)) + 
  geom_bar(stat = "identity", position = "dodge") +
  facet_grid(metric~., scales = "free_y")

您可以在log10-scale 上比较不同的值:

ggplot(df, aes(x = categories, y = value, fill = metric)) + 
  geom_bar(stat = "identity", position = "dodge") +
  scale_y_log10(name = "value (log10 scale)", 
                breaks = 10^(0:10), labels = paste0(10, "^", 0:10))

【讨论】:

  • 啊,我误会了。我也已经考虑过 facet_grid。但我想展示页面和产品之间的关系......但也许 facet_grid 是这里最好的解决方案,因为尺度是不同的......谢谢你的帮助!
  • 谢谢@kath!第二个很棒:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 2012-08-18
  • 1970-01-01
  • 2018-06-23
相关资源
最近更新 更多