我正在尝试用R中的情节创建一个分组条形图。我可以使用ggplot创建我想要的但我不能使用ggplotly因为它没有正确显示负数的问题({{3} })。我有两个问题希望你可以提供帮助:
到目前为止,这是我的代码:
# Create example data
example_data <- as.data.frame(matrix(NA, 9, 3))
names(example_data) <- c("group", "subgroup", "value")
example_data$group <- c("Gender", "Gender", "Race", "Race", "Race", "Age", "Age", "Age", "Human")
example_data$subgroup <- c("Female", "Male", "Asian", "Black", "White", "Young", "Middle", "Old", "Yes")
example_data$value <- c(11, 14, 72, 12, 82, 22, 30, 4, 5)
# Create chart
p <- plot_ly(
data = example_data,
x = ~group,
y = ~value,
color = ~subgroup,
type = "bar"
) %>%
layout(
# xaxis = list(type="category"), # Doesn't seem to do anything
# barmode = "group", # Doesn't seem to do anything
showlegend = FALSE
) %>%
add_annotations(text = ~subgroup,
x = ~group,
y = ~value / 2, # Center vertically on the bar
# xref = "x", # Doesn't seem to do anything
# yref = "y", # Doesn't seem to do anything
# xanchor = 'center', # Doesn't seem to do anything
# yanchor = 'bottom', # Doesn't seem to do anything
showarrow = FALSE)
p
答案 0 :(得分:0)
如果仍然感兴趣,我发现了以下参数:xanchor ='right',xshift = -10(向左移动,-10与10像素有关)与向右移动标签相同-> xanchor ='left',xshift = 10
https://github.com/plotly/plotly.js/blob/master/src/components/annotations/attributes.js
但是注释需要分开: add_annotations(text = Male,xshift = -10 .... add_annotations(text = Female,xshift = 10 ....