如何在R中复制此Excel 3D直方图

时间:2018-08-21 22:37:17

标签: r ggplot2 plot 3d histogram

我希望在R中大致复制以下3d直方图(在Excel中制作)。窍门是,标签应位于大法官姓名的之间,因为条形图用于描绘数字有时候,某些法官投票反对其他法官。 enter image description here

您可以使用以下代码生成数据:

cutpoints <- c(0, 22, 16, 12, 13, 7, 16, 13, 20)

justice_names <- c("Peckham", "Brewer", "Shiras", 
    "Fuller", "Gray", "Brown", "McKenna", "White", "Harlan")  

1 个答案:

答案 0 :(得分:0)

这是一个粗略的复制品,省略了3D。

将x轴绘制为数字并在之后应用标签,可以将标签向右移动半个标记。

library(ggplot2)

cutpoints <- c(0, 22, 16, 12, 13, 7, 16, 13, 20)

justice_names <- c("Peckham",
                   "Brewer",
                   "Shiras",
                   "Fuller",
                   "Gray",
                   "Brown",
                   "McKenna",
                   "White",
                   "Harlan")

hist_data <- data.frame(justice_names, cutpoints, order = seq(1:9))

hist_data$justice_names <-
  factor(hist_data$justice_names, levels = hist_data$justice_names)

ggplot(hist_data, aes(x = order, y = cutpoints)) +
  geom_bar(stat = "identity", width = 0.5) +
  scale_x_continuous(breaks = hist_data$order + 0.5, # Shift labels by 0.5
                     labels = hist_data$justice_names) +
  geom_text(aes(label = cutpoints), vjust = 0, nudge_y = 0.25) +
  labs(x = "Justice", y = "Number",
       title = "Fig A-1. Number of Cutpoints, 1899-1901") +
  theme(panel.grid.minor = element_blank(),
        panel.grid.major.x = element_line(linetype = "dashed"))