用ggplot和plotly调整文本的位置

时间:2016-08-19 06:53:05

标签: r ggplot2 plotly

我无法将地图从ggplot打印到plotly,并保持良好的文字位置。

数据示例:

library(ggplot2)
library(plotly)
library(dplyr)
library(reshape2)

#mock data
df1 <- data.frame( 
  Gruppering2     = factor(c("Erhverv Erhverv Salg","Erhverv Erhverv Salg","Erhverv Erhverv Salg")),
  periode    = factor(c("Denne maaned","Denne uge", "I gaard")),
  Answer_rate = c(0.01,0.4,0.7),
  SVL    = c(0.40,0.43,0.67),
  over_180     = c(0.5,0.7,0.3)  
)

#color 
plotCol <- c( rgb(44,121,91, maxColorValue = 255),  rgb(139,0,0, maxColorValue = 255),rgb(0,0,139, maxColorValue = 255)) 

#plot code
dfpct <- melt(df1[,c(2,3,4,5)], id.vars = "periode",
              measure.vars = c( "Answer_rate","SVL", "over_180"), 
              variable.name = "P", value.name = "value")
dfpct <- na.omit(dfpct)

pct <- ggplot(dfpct, aes(x = periode, y = value, fill = P, group = P, width = 0.6)) + 
  geom_bar(stat = "identity", position="dodge", colour = "black", width = 0.7, show.legend = FALSE) +
  labs(x = NULL, y = "Calls") +
  #ggtitle("Forecast Error") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        plot.title = element_text(size = rel(1.2), face = "bold", vjust = 1.5),
        axis.title = element_text(face = "bold"),
        axis.text = element_text(),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.key.width = unit(2, "lines"),
        legend.key.height = unit(0.5, "lines"),
        legend.title = element_blank()) +
  geom_text(aes(label=paste(value*100,"%",sep="")), position = position_dodge(width=0.6), vjust = -0.5 ) +
  scale_fill_manual(values = plotCol)

pct # the is perfectly located above

ggplotly(pct, textposition = 'top center') # text crosses over the bars

正如您所看到的那样 - ggplot效果很好 - 但是当我转换为plotly时,文本会被移动。我已尝试在ggplot和plotly中使用各种设置,但还没有运气。

2 个答案:

答案 0 :(得分:4)

看起来 vjust 无法识别,但可能在路线图上。来自GitHub:

# convert ggplot2::element_text() to plotly annotation
make_label <- function(txt = "", x, y, el = ggplot2::element_text(), ...) {
  if (is_blank(el) || is.null(txt) || nchar(txt) == 0 || length(txt) == 0) {
    return(NULL)
  }
  angle <- el$angle %||% 0
  list(list(
    text = txt,
    x = x,
    y = y,
    showarrow = FALSE,
    # TODO: hjust/vjust?
    ax = 0,
    ay = 0,
    font = text2font(el),
    xref = "paper",
    yref = "paper",
    textangle = -angle,
    ...
  ))
}

最简单的方法可能是在 geom_text 中分配 y 值,但您在高度上会丢失一些缩放。

pct <- ggplot(dfpct, aes(x = periode, y = value, fill = P, group = P, width = 0.6)) + 
  geom_bar(stat = "identity", position="dodge", colour = "black", width = 0.7, show.legend = FALSE) +
  labs(x = NULL, y = "Calls") +
  theme_bw() +
  theme(panel.grid.major = element_blank(),
        plot.title = element_text(size = rel(1.2), face = "bold", vjust = 1.5),
        axis.title = element_text(face = "bold"),
        axis.text = element_text(),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.key.width = unit(2, "lines"),
        legend.key.height = unit(0.5, "lines"),
        legend.title = element_blank()) +
  geom_text(aes(label=paste(value*100,"%",sep=""), y = value+0.01), position = position_dodge(width = 0.6)) +
  scale_fill_manual(values = plotCol)

ggplotly(pct)

enter image description here

或者,如果您知道最终输出的尺寸,则可以编辑plotly_build对象的组件:

gg <- plotly_build(pct)
gg$data[[4]]$y <- gg$data[[4]]$y+0.006
gg$data[[5]]$y <- gg$data[[5]]$y+0.006
gg$data[[6]]$y <- gg$data[[6]]$y+0.006

答案 1 :(得分:1)

我遇到了同样的问题,我通过向 y轴添加一个值来解决它,但是为了避免缩放问题,我添加了最小值的百分比。您可以根据数据本身进行调整。希望对您有所帮助:

geom_text(aes(label=paste(value*100,"%",sep=""), y = value+0.1*min(value), position = position_dodge(width = 0.6))