我必须将ggplot
转换为plotly
。自然,最简单的方法似乎是使用ggploty
,但是字体大小会发生变化。返回到轴标签和标题的合适大小没有问题,但是我无法弄清楚数据标签该怎么做?有任何想法吗?我确实更喜欢保留ggplot
并用ggplotly
包裹(还有许多其他图)。
我从布局中尝试了字体,但没有成功:
library(ggplot2)
library(plotly)
tbl <- data.frame(
indicateur = c("freq1", "freq2", "freq3" ),
valeur = c(44, 78, 84)
)
ggplotly(
ggplot(tbl) +
geom_bar(aes(x = indicateur, y = valeur), stat = 'identity', fill = rgb(31, 119, 180, maxColorValue = 255)) +
geom_text(aes(x = indicateur, y = valeur, label = valeur), vjust = -0.5) +
ylim(0, max(tbl$valeur)*1.1) +
labs(x = "", y = "", title = "simple counting") +
theme_bw() +
theme(
axis.line = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0.5)
)
) %>%
layout(
showlegend = FALSE,
textfont = list(size = 5),
titlefont = list(size = 12),
xaxis = list(tickfont = list(size = 9))
)
预先感谢您的帮助。
答案 0 :(得分:0)
仅将size
传递到geom_text
怎么样?另外,我认为所有的布局调整都是不必要的。请参阅以下内容:
library(tidyverse)
library(plotly)
tbl <- data.frame(
indicateur = c("freq1", "freq2", "freq3" ),
valeur = c(44, 78, 84)
)
p_size_5 <- ggplot(tbl, aes(x = indicateur, y = valeur, label = valeur)) +
geom_bar(stat = "identity", fill = rgb(31, 119, 180, maxColorValue = 255)) +
geom_text(vjust = -0.5, size = 5) +
labs(
title = "simple counting (text 5)",
x = NULL, y = NULL
) +
theme_bw() +
theme(
axis.line = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0.5)
)
ggplotly(p_size_5)
p_size_2 <- ggplot(tbl, aes(x = indicateur, y = valeur, label = valeur)) +
geom_bar(stat = "identity", fill = rgb(31, 119, 180, maxColorValue = 255)) +
geom_text(vjust = -0.5, size = 2) +
labs(
title = "simple counting (text 2)",
x = NULL, y = NULL
) +
theme_bw() +
theme(
axis.line = element_blank(),
panel.border = element_blank(),
plot.title = element_text(hjust = 0.5)
)
ggplotly(p_size_2)