我想使用 R 和 Highcharter 创建一个图,并在其上放置一个连接到两个点(而不仅仅是一个点)的注释。
这就是我尝试的:
# Laden der Bibliothek
library(highcharter)
# Laden der Titanic-Daten aus dem 'datasets'-Paket
data <- datasets::Titanic
# Umstrukturierung der Daten für den Plot
data_long <- as.data.frame(as.table(data))
colnames(data_long) <- c("Class", "Sex", "Age", "Survived", "Count")
data_long$Count <- as.numeric(data_long$Count)
# Filtern der Daten für Erwachsene
data_long <- subset(data_long, Age == "Adult")
# Erstellen des gestapelten Balkendiagramms
hchart(data_long, "column", hcaes(x = Sex, y = Count, group = interaction(Survived, Class)), stack = TRUE) %>%
hc_plotOptions(column = list(stacking = "percent"))%>%
hc_annotations(list(
labels = list(
list(
point = list(x = 0.5, y = 60, xAxis = 0, yAxis = 0), # Zwischen den Bars
text = "Differenz: 12%",
style = list(fontSize = "11px") # Optional: Anpassen der Schriftgröße
)
)
))
结果:
这是一个(hacky)选项,使用
shape="connector"
绘制连接线,并使用 allowOverlap=TRUE
使用透明标签连接到第二个点:
library(highcharter)
# Erstellen des gestapelten Balkendiagramms
hchart(data_long, "column", hcaes(
x = Sex, y = Count,
group = interaction(Survived, Class)
), stack = TRUE) %>%
hc_plotOptions(column = list(stacking = "percent")) |>
hc_annotations(
list(
labelOptions = list(
shape = "connector",
allowOverlap = TRUE
),
labels = list(
list(
point = list(
x = 1,
y = 35,
xAxis = 0, yAxis = 0
),
text = "Differenz: 12%",
style = list(fontSize = "11px"),
x = -130,
y = -120
),
list(
point = list(
x = 0,
y = 65,
xAxis = 0, yAxis = 0
),
text = "Differenz: 12%",
style = list(fontSize = "11px", color = "transparent"),
x = 130,
y = -15
)
)
)
)