【问题标题】:Change boxplot x axis labels alignment in r -ggplot2在 r -ggplot2 中更改箱线图 x 轴标签对齐方式
【发布时间】:2014-08-18 21:39:26
【问题描述】:

我是使用 RStudio 的新手,所以如果我忽略了解决问题的明显方法,请原谅我。

我在 r 中制作了一个几乎正确的箱线图,除了我想更改 x 轴标签/文本的对齐方式/或大小。

我目前使用的是 RStudio 版本 3.1,但自 R 版本 2.13.1 (2011-07-08) 以来发生了一些变化平台:x86_64-pc-mingw32/x64 (64-bit) as code from then, is not working对我来说。

有 2 个物种和 5 个处理,目的是绘制一个具有物种和处理之间相互作用的箱线图,排列成物种 1 治疗 1,物种 2 治疗 1,物种 1 治疗 2,物种 2 治疗2等

目前有效的代码是:

pseudodata <- read.csv(file.choose(), header=TRUE)
pseudodata$agg_count <- rep(1:60, each=2)

data <- aggregate(pseudodata , by=list(pseudodata$agg_count), FUN=mean)

boxplot   (Force..N. ~ interaction(data$Species, data$Treatment), 
          data=data, col=(c("gold","darkgreen")),
          xlab="Species and Treatment", ylab="Force of detachment (N)")

这产生了一个与Second boxplot with colour notched 非常相似的箱线图

但是 x 轴文本标签并未全部显示,所以我想将它们设置为 45o 角。

我试过了:

labels <- c("Helix.Copper", "Hibernica.Copper","Helix.Easy on", "Hibernica.Easy on",
            "Helix.Pegagraff", "Hibernica.Pegagraff", "Helix.Untreated", 
            "Hibernica.Untreated", "Helix.Zinc", "Hibernica.Zinc")

forceplot <- boxplot(Force..N. ~ interaction(data$Species, data$Treatment), 
        data=data,  # dataframe used
        xaxt="n", # suppress the default x axis
        col=(c("gold","darkgreen")),
        xlab="Species and Treatment", ylab="Force of detachment (N)")

Text(forceplot, par(“usr”)[3], labels = labels, srt = 45, 
        adj = c(1.1,1.1), xpd = TRUE, cex=.9)
        )
axis(2)

但是似乎不起作用,我不知道为什么。如果有人对如何创建这个情节有任何想法,我将不胜感激。

【问题讨论】:

    标签: r plot boxplot axis-labels


    【解决方案1】:

    我不确定这是否是您想要的。但是这种方法对您有帮助吗?

    library(ggplot2)
    
    ### Create a sample data
    species <- rep(c("s1","s2"), each = 20, times = 5)
    treatment <- rep(c("t1","t2","t3","t4","t5"), each = 1, times = 40)
    value <- runif(200, 100, 500)
    
    foo <- data.frame(cbind(x,y,value))
    foo$value <- as.numeric(foo$value)
    
    ### Draw boxplots
    ana <- ggplot(foo, aes(x = species, y = value, fill = treatment)) +
        geom_boxplot()
    
    ana
    

    【讨论】:

      猜你喜欢
      • 2016-09-03
      • 2013-10-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多