我正在尝试创建一个仪表图,如下图所示:
df 为:
df <- structure(list(Genero = c("Hombre", "Mujer"), Media = c(46.7,
53.3)), row.names = c(NA, -2L), class = c("etable", "data.frame"
))
我一直在尝试的代码是:
stops <- list_parse2(df)
highchart() %>%
hc_chart(type = 'solidgauge') %>%
hc_pane(
startAngle = -90,
endAngle = 90,
background = list(
outerRadius = '100%',
innerRadius = '60%',
shape = 'arc'
)
) %>%
hc_yAxis(
stops = stops,
lineWidth = 0,
minorTickWidth = 0,
tickAmount = 2,
min = 0,
max = 100,
labels = list(y = 25)
) %>%
hc_add_series(
data = df$Media,
color = '#EB6909'
) %>%
hc_exporting(enabled = TRUE) %>%
hc_plotOptions(series = list(animation = FALSE))
我认为这就是您正在寻找的:
df <- df |>
dplyr::mutate(color = c('#FFA500', '#A9A9A9'))
pie <- df |>
hchart(type = "pie",
hcaes(x = Genero, y = Media, color = color)) |>
hc_plotOptions(
pie = list(
innerSize = '70%',
startAngle = -90,
endAngle = 90,
dataLabels = list(
enabled = TRUE,
format = '<b>{point.name}</b>: {point.percentage:.1f} %'
)
)
)
pie
这个想法是从圆环图中即兴制作一个仪表图。首先,我在数据框中创建了一个新列
color
,用它为仪表图着色。接下来,在 hc_plotOptions()
的饼图列表中,我将 innerSize
参数设置为 70% 以在圆环图中创建孔,然后将 startAngle
和 endAngle
分别设置为 -90 和 90 以有效地转动将图表转换为仪表图。
我希望这对你有用!
有这样的事吗?
install.packages("tidyverse")
library(tidyverse)
your_df <- tibble(
part = c("Hombre", "Mujer"),
percentage = c(0.467, 1 - 0.467),
start = lag(percentage, default = 0) * pi,
end = start + percentage * pi
)
your_df
install.packages("ggforce")
library(ggforce)
gPlot <- your_df |>
ggplot() +
geom_arc_bar(
aes(
x0 = 1,
y0 = 1,
fill = part,
start = start - pi / 2,
end = end - pi / 2,
r0 = 0.75,
r = 1
)
) +
coord_fixed() +
theme_void() +
theme(
legend.position = "none",
plot.title = element_text(
family = "Source Sans Pro", size = 2, face = "bold"
)
) +
annotate(geom = "text", x = .3, y = 2, hjust = 0, label = "Hombre: 46.7%", color = "black", fontface = "bold", size = 3) +
annotate(geom = "text", x = 1.4, y = 2, hjust = 0, label = "Mujer: 53.3%", color = "black", fontface = "bold", size = 3)
gPlot