Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tidyverse自定义圆形条形图_R_Ggplot2_Aes - Fatal编程技术网

使用tidyverse自定义圆形条形图

使用tidyverse自定义圆形条形图,r,ggplot2,aes,R,Ggplot2,Aes,我正在按照中的教程进行循环条形图定制,并根据我的数据进行了调整,因为我希望看到每个主题的5个最高成本。这是我的数据结构: > dput(dtd4) structure(list(topic = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,

我正在按照中的教程进行循环条形图定制,并根据我的数据进行了调整,因为我希望看到每个主题的5个最高成本。这是我的数据结构:

> dput(dtd4)
structure(list(topic = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 
8L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L), .Label = c("Topic 1", 
"Topic 10", "Topic 2", "Topic 3", "Topic 4", "Topic 5", "Topic 6", 
"Topic 7", "Topic 8", "Topic 9"), class = "factor"), parent = c("Jhon", 
"Mary", "Sean", "Lissa", "Tatiana", "Jhon", "Mary", "Sean", "Lissa", 
"Tatiana", "Jhon", "Mary", "Sean", "Lissa", "Tatiana", "Jhon", 
"Mary", "Sean", "Lissa", "Tatiana", "Jhon", "Mary", "Sean", "Lissa", 
"Tatiana", "Jhon", "Mary", "Sean", "Lissa", "Tatiana", "Jhon", 
"Mary", "Sean", "Lissa", "Tatiana", "Jhon", "Mary", "Sean", "Lissa", 
"Tatiana", "Jhon", "Mary", "Sean", "Lissa", "Tatiana", "Jhon", 
"Mary", "Sean", "Lissa", "Tatiana"), cost = c(9671, 9671, 9671, 
9671, 9671, 19842, 20704, 19842, 19842, 19449, 21448, 20251, 
21448, 21448, 21448, 21448, 19842, 19842, 19842, 19842, 19842, 
19842, 19842, 19842, 19842, 19842, 19842, 22031, 19842, 19842, 
19842, 19842, 19842, 19842, 19842, 19842, 19842, 19842, 19842, 
19842, 19842, 19842, 19842, 19842, 19842, 21448, 21448, 21448, 
21448, 22132)), row.names = c(NA, -50L), class = "data.frame")
下面的代码是我调整后使用的代码

library(tidyverse)

# Set a number of 'empty bar' to add at the end of each group
empty_bar <- 3
to_add <- data.frame( matrix(NA, empty_bar*nlevels(dtd4$topic), ncol(dtd4)) )
colnames(to_add) <- colnames(dtd4)
to_add$topic <- rep(levels(dtd4$topic), each=empty_bar)
dtd4 <- rbind(dtd4, to_add)
dtd4 <- dtd4 %>% arrange(topic)
dtd4$id <- seq(1, nrow(dtd4))


# Get the name and the y position of each label
label_data <- dtd4
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
label_data$hjust <- ifelse( angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)

# prepare a data frame for base lines
base_data <- dtd4 %>% 
  group_by(topic) %>% 
  summarize(start=min(id), end=max(id) - empty_bar) %>% 
  rowwise() %>% 
  mutate(title=mean(c(start, end)))

# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]

# Make the plot
p <- ggplot(dtd4, aes(x=as.factor(id), y=cost, fill=topic)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar

  geom_bar(aes(x=as.factor(id), y=cost, fill=topic), stat="identity", alpha=0.5) +

  # Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
  geom_segment(data=grid_data, aes(x = end, y = 80, xend = start, yend = 80), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 60, xend = start, yend = 60), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 40, xend = start, yend = 40), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
  geom_segment(data=grid_data, aes(x = end, y = 20, xend = start, yend = 20), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +

  # Add text showing the value of each 100/75/50/25 lines
  annotate("text", x = rep(max(data$id),4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80") , color="grey", size=3 , angle=0, fontface="bold", hjust=1) +

  geom_bar(aes(x=as.factor(id), y=cost, fill=topic), stat="identity", alpha=0.5) +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    legend.position = "none",
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-1,4), "cm") 
  ) +
  coord_polar() + 
  geom_text(data=label_data, aes(x=id, y=cost+10, label=parent, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +

  # Add base line information
  geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE )  +
  geom_text(data=base_data, aes(x = title, y = -18, label=topic), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)

p

我迷路了,因为每次我检查时,每个数据集都会给出类似的输出,但当通过“p”时,有些地方出错了

基本数据的长度与绘图最后一行中提供给
hjust
的向量长度不同。将其替换为以下内容,其中hjust是与数据长度相同的向量:

geom_text(data=base_data, aes(x = title, y = -18, label=topic), hjust=c(rep(0,5),rep(1,5)), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)
您可能需要调整
hjust
向量以提供所需内容,但这会让您顺利完成任务


此外,您没有提供
数据
,因此“我的输出”无法工作。希望您知道
数据
是在哪里获取的
max(data$id)

您能提供
dtd4
数据的示例吗?(使用
dput(dtd4)
)@ErrorJordan这会有帮助吗?样本数据中的某些内容不太正确,不可复制。请再试一次?再仔细检查一下它是否适合你,现在呢?
geom_text(data=base_data, aes(x = title, y = -18, label=topic), hjust=c(rep(0,5),rep(1,5)), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)