如何在绘图中使用 R 获得按州分组的条形图?
我想要的结果应该像这个用 excel 制作的示例图表:
我的数据:
data <- data.frame(
State = c(
"Tennessee", "Tennessee", "Tennessee", "Tennessee",
"Kentucky", "Kentucky", "Kentucky", "Kentucky", "Kentucky",
"Georgia", "Georgia", "Georgia"
),
City = c(
"Chattanooga", "Knoxville", "Memphis", "Nashville",
"Covington", "Owensboro", "Bowling Green", "Lexington", "Louisville",
"Columbus City", "Augusta", "Atlanta City"
),
Population = c(
177571, 186239, 652717, 660388,
40640, 57265, 58067, 295803, 597337,
189885, 195844, 420033
)
)
我的代码:
plot_ly() %>%
add_trace(
x = ~City,
y = ~Population,
type = 'bar',
name = 'Population')