【问题标题】:ggplot2: Error: Discrete value supplied to continuous scaleggplot2:错误:提供给连续刻度的离散值
【发布时间】:2016-07-26 21:58:11
【问题描述】:

对于示例数据框:

df1 <- structure(list(country.name = structure(c(4L, 11L, 10L, 2L, 1L, 
       3L, 8L, 5L, 7L, 9L, 6L), .Label = c("Austria", "Belgium", "Czech Republic", 
       "Denmark", "France", "Germany", "Netherlands", "Norway", "Poland", 
       "Sweden", "Switzerland"), class = "factor"), level = c(2L, 2L, 
       2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), no.regions = c(5L, 7L, 8L, 
       11L, 9L, 8L, 7L, 16L, 12L, 15L, 14L), min_result = c(42.59, 33.57, 
       43.1, 38.46, 41.76, 44.05, 41.67, 36.32, 36.18, 42.79, 39.91), 
       max_result = c(50.24, 46.56, 58.24, 57.41, 61.07, 64.56, 
       63.25, 58.19, 59.14, 69.19, 67.11), diff = c(7.65, 12.99, 
       15.14, 18.95, 19.31, 20.51, 21.58, 21.87, 22.96, 26.4, 27.2
       ), RD = c(-0.07, 0.131, -0.091, -0.153, -0.172, 0.203, -0.166, 
       0.145, -0.228, -0.266, -0.261), RDCI_lower = c(-0.21, -0.028, -0.194, -0.328, -0.376, 0.076, -0.315, 0.075, -0.407, -0.348, 
       -0.347), RDCI_upper = c(0.07, 0.29, 0.012, 0.021, 0.031, 
      0.331, -0.017, 0.216, -0.049, -0.184, -0.175), RDpvalue = c(0.3237, 
      0.1113, 0.08, 0.0829, 0.1017, 0.0023, 0.0299, 0, 0.0149, 
      0, 0), diff_order = structure(1:11, .Label = c("Denmark", 
       "Switzerland", "Sweden", "Belgium", "Austria", "Czech Republic", 
      "Norway", "France", "Netherlands", "Poland", "Germany"), class = "factor", scores = structure(c(19.31, 
      18.95, 20.51, 7.65, 21.87, 27.2, 22.96, 21.58, 26.4, 15.14, 
      12.99), .Dim = 11L, .Dimnames = list(c("Austria", "Belgium", 
      "Czech Republic", "Denmark", "France", "Germany", "Netherlands", 
      "Norway", "Poland", "Sweden", "Switzerland"))))), .Names = c("country.name", 
      "level", "no.regions", "min_result", "max_result", "diff", "RD", 
      "RDCI_lower", "RDCI_upper", "RDpvalue", "diff_order"), row.names = c(NA, 
      -11L), class = "data.frame")

我正在尝试使用 ggplot2 创建绘图:

library(ggplot2)

df1$diff_order <- reorder(df1$country.name, df1$diff) #Set order of countries by diff (if they are not already), and then set this to be plot 

a <- ggplot(df1, aes((x=diff_order), y=country.name,fill=level)) +
  geom_bar(stat="identity") +
  xlab("Country") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values=c("#009E73", "#0072B2"),name="NUTS level") +
  ylim(0, 40) +
  ggtitle("All")  +
  theme(plot.title = element_text(hjust = 0)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=10,face="bold"))

a

但是,我得到了错误:

错误:提供给连续刻度的离散值

我知道此错误已被广泛报告 elsewhere,但是,我找不到解决问题的适当方法。据我了解,R 在我的一个秤上遇到问题,但是,我可以尝试更改他们的课程(例如 df1$country.name &lt;- as.character(df1$country.name) df1$level &lt;- as.factor(df1$level)),但我仍然遇到同样的错误。

有人有什么想法吗?

更新

如果我按照@MLavoie 的建议更改代码...

a <- ggplot(df1, aes((x=diff_order), y=country.name,fill=as.factor(level))) +
  geom_bar(stat="identity") +
  xlab("Country") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values=c("#009E73", "#0072B2"),name="NUTS level") +
  ggtitle("All")  +
  theme(plot.title = element_text(hjust = 0)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.text=element_text(size=6),
        axis.title=element_text(size=10,face="bold"))

我收到以下错误:

geom_bar(stat = "identity") + xlab("Country") 中的错误: 二元运算符的非数字参数

我做错了什么?

到目前为止,我可以生成此图表,但我希望仅在 y 轴上显示国家(按顺序),在 x 轴上显示“差异”。有什么想法吗?

【问题讨论】:

  • 使用 fill=as.factor(level)) 并删除 ylim(0, 40) 应该可以解决您的问题
  • 谢谢@MLavoie。我尝试了您的建议,但仍然出现(不同的)错误 - 我做错了什么?
  • ggplot(df1, aes((x=diff_order), y=country.name,fill=as.factor(level)))) 末尾有一个虚假的)
  • 谢谢,@bnord。这会打印图表,但国家/地区不按顺序排列,并且我在 x 轴(仅国家/地区)上没有选择轴(差异)。当我尝试在 y 轴上设置限制时,我得到了原始错误...
  • 你真的想要ggplot(df1, aes(x=diff, y=country.name,fill=as.factor(level)))吗?

标签: r ggplot2


【解决方案1】:

最后,我设法让它工作了......所以感谢那些给我cmets的人:

df1$diff_order <- reorder(df1$country.name, df1$diff) #Set order of countries by diff, and then set this to be plot 

a <- ggplot(df1, aes((x=diff_order), y=diff,fill=as.factor(level))) +
  geom_bar(stat="identity") +
  xlab("Country") +
  theme_classic() +
  coord_flip() +
  scale_fill_manual(values=c("#009E73", "#0072B2"),name="NUTS level") +
  ggtitle("All")  +
  theme(plot.title = element_text(hjust = 0)) +
  theme(axis.title.x = element_blank()) +
  theme(axis.text=element_text(size=9),
        axis.title=element_text(size=10,face="bold"))

a

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 2019-01-04
    • 1970-01-01
    • 2015-01-08
    • 2023-01-31
    • 2015-09-26
    • 1970-01-01
    • 2020-11-10
    • 2014-11-14
    相关资源
    最近更新 更多