【问题标题】:superimpose barchart with stripplot用条形图叠加条形图
【发布时间】:2020-03-06 17:47:33
【问题描述】:

我想用条形图叠加条形图,并在xyplot中使用面板参数,但它不起作用。它不绘制任何内容并发出许多警告,例如:

1: In order(as.numeric(x)) : NAs introduced by coercion
2: In diff(as.numeric(x[ord])) : NAs introduced by coercion

代码在这里,但它适用于阶乘交替和主题变量,尽管它以水平方式绘制条形:

t2 <- data.frame(Alternative = c("FA", "HF", "NO", "HF", "FA", "NO"), 
  AlloOthe = c(50, 80, 20, 80, 80, 20), 
  Subject = rep(c("2001", "2002"), each = 3)
)
t2$Alternative <- as.character(t2$Alternative)
t2$Subject <- as.character(t2$Subject)
library(lattice)
xyplot(AlloOthe ~ Alternative | Subject, data = t2,
  panel = function(x, y) {
    panel.barchart(x, y)
    panel.stripplot(x, y, type = c("p", "l"))
  },
  ylab = "Allocation to Other"
)

那么有没有人可以向我展示正确的代码或让它在正确的数据上工作?非常感谢!

【问题讨论】:

    标签: r panel lattice


    【解决方案1】:

    我不是lattice 软件包的专家,但我通过以下帖子了解您的情节:Add stripplot points to bwplot (Lattice graphics in R),因此,我将... 添加到面板函数定义中,并将xyplot 替换为@987654329 @。

    所以,基本上,它看起来像:

    # Assigning Alternative and Subject as factor
    t2$Alternative = as.factor(t2$Alternative)
    t2$Subject = as.factor(t2$Subject)
    
    library(lattice)
    barchart(AlloOthe ~ Alternative | Subject, data = t2,
             panel = function(x,y,...) {
               panel.barchart(x,y,col=adjustcolor("darkorchid",alpha=.6),...)
               panel.stripplot(x,y,col="lightseagreen",pch=16,size=3,...)
             },
             ylab = "Allocation to Other")
    

    情节是这样的:

    作为替代方案,您可以通过执行以下操作来使用ggplot

    library(ggplot2)
    ggplot(t2, aes(x = Alternative, y = AlloOthe, group = Alternative)) +
      facet_grid(.~Subject) +
      geom_bar(stat = "identity", fill = adjustcolor("darkorchid", alpha = 0.6)) +
      geom_point(size = 3, color = "lightseagreen") +
      ylab("Allocation to Other")
    
    

    情节是这样的:

    希望对你有帮助!

    【讨论】:

    • 看来function(x, y, ...) 中的... 和以下雄蕊对于使用阶乘替代作为x 变量进行正确绘图是必要的。并且 barchart 而不是 xyplot 是正确使用的功能。非常感谢。你很高兴在这里提供 lattice 和 ggplot 方法。
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 2018-01-06
    • 2018-10-20
    相关资源
    最近更新 更多