【问题标题】:Customizing the angle in a ggplot x-axis tick labels自定义ggplot x轴刻度标签中的角度
【发布时间】:2018-11-13 18:05:02
【问题描述】:

我想使用ggplot2 绘制heatmap,并且我想自定义 x 轴刻度 - 位置、文本和角度。

这是我的示例数据:

set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))

我想让 x 轴刻度只有一个刻度值,位于轴范围的中间 - 10,标签为:“X-Label”,呈 90 度角。另外,我不想要 x 轴标题。

这是我正在尝试的:

library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
  geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
  scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))

所以,显然它不起作用。

我认为我可以将角度定义为 scale_x_discrete 参数的一部分,但找不到该选项。

另外,请注意,我不想要 x 轴标题,而只想要刻度文本。所以我不确定为什么scale_x_discrete 中的breakslabels 参数会被忽略。

这是使用plotly 的预期结果:

library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
  layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))

谢谢

【问题讨论】:

  • 由于您的描述不太清楚,您能发布想要的结果吗?这可能涉及使用图像软件为我们绘制草图:Paint、Paintbrush、Pinta 等。

标签: r ggplot2 axis-labels


【解决方案1】:

我认为指定breaks = 10 会导致您的轴标签不打印。 10 不是您的值之一,它不符合?scale_x_discrete 中有效参数的描述,所以让我们使用真正的 x 轴值。您说“就在中间”,所以我将使用 "G50",因为您的 x 轴值范围从 "G1""G100"

ggplot(data = df,
       mapping = aes(x = Var1, y = Var2, fill = value)) +
  geom_tile() +
  theme_minimal() +
  scale_fill_gradient2(
    name = "Scaled Value",
    low = "darkblue",
    mid = "gray",
    high = "darkred"
  ) +
  scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") + 
  theme(
    axis.text.x = element_text(
      angle = 90,
      hjust = 1,
      vjust = 0.5
  ))

我还在scale 函数中指定了name = "",而不是使用theme 来禁用轴标题。

如果需要动态计算中间层,虽然有点乱,但是应该可以:

breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]

【讨论】:

    【解决方案2】:

    scale_x_discrete 中的中断选项不指定中断的数量,而是指定以下之一:

    • NULL 表示没有中断
    • waiver() 用于转换对象计算的默认中断
    • 中断的字符向量
    • 将限制作为输入并返回中断作为输出的函数

    我发现定义字符向量是最简单的。在这种情况下,第 50 个元素的一半。

    set.seed(1)
    df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
    
    library(ggplot2)
    ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
      geom_tile() +
      theme_minimal() +
      scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
      scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
      theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
    
    #Use this line instead to label along the axis
     #scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
    

    希望这是您正在寻找的。​​p>

    【讨论】:

      猜你喜欢
      • 2013-07-19
      • 2012-05-08
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多