我收到一些缺少值的警告,无法找出原因。除此之外,我还试图在小节之间添加空间,因为事情也变得重叠了
data=data.frame(
individual = tmlsTotal$screen_name,
group = c( rep('1', quantidadeSemana1), rep('2', quantidadeSemana2), rep('3', quantidadeSemana3), rep('4', quantidadeSemana4)),
value = tmlsTotal$total
)
# 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(data$group), ncol(data)) )
colnames(to_add) = colnames(data)
to_add$group=rep(levels(data$group), each=empty_bar)
data=rbind(data, to_add)
data=data %>% arrange(group)
data$id=seq(1, nrow(data))
# Get the name and the y position of each label
label_data=data
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=data %>%
group_by(group) %>%
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
y <- ggplot(data, aes(x=as.factor(id), y=value, fill=group)) + # 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=value, fill=group), stat="identity", alpha=0.5, position = position_dodge(width = 0.9)) +
geom_segment(data=grid_data, aes(x = end, y = 2000, xend = start, yend = 2000), colour = "#e65100", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 1500, xend = start, yend = 1500), colour = "#e65100", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 1000, xend = start, yend = 1000), colour = "#e65100", alpha=1, size=0.3 , inherit.aes = FALSE ) +
geom_segment(data=grid_data, aes(x = end, y = 500, xend = start, yend = 500), colour = "#e65100", 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(500, 1000, 1500, 2000), label = c("500", "1000", "1500", "2000") , color="#f44336", size=3 , angle=0, fontface="bold", hjust=1) +
geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
ylim(-5000,3300) +
theme_minimal() +
theme(
legend.position = "right",
axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank(),
plot.margin = unit(rep(-1,4), "cm")
) +
#scale_fill_manual(values = c("#3e2723","#4a148c","#00bfa5","#1b5e20")) +
coord_polar() +
##nomes na barra
geom_text(data=label_data, aes(x=id, y=value+10, label=paste(individual, value), hjust=hjust), color="black", fontface="bold",alpha=0.6, size=3, angle= label_data$angle, inherit.aes = FALSE, family="sans" ) +
#scale_fill_discrete("Semanas", labels=c("Primeira Semana", "Segunda Semana", "Terceira Semana", "Quarta Semana"))+
# primeiro é a linha, segundo a legenda de cada grupo
geom_segment(data=base_data, aes(x = start, y = -10, xend = end, yend = -10), colour = "black", alpha=0.8, size=0.8 , inherit.aes = FALSE ) +
geom_text(data=base_data, aes(x = title, y = -150, label=group), hjust=c(1,1,0,0), colour = "#424242", alpha=0.8, size=3.5, fontface="bold", inherit.aes = FALSE) +
scale_fill_manual(values = c("#880e4f","#aa00ff","#311b92","#0d47a1"), "Semanas - 2019", labels=c(paste(format(semana1Fim, "%d/%m")," a ", format(semana1Inicio, "%d/%m")), paste(format(semana2Fim, "%d/%m")," a ", format(semana2Inicio, "%d/%m")), paste(format(semana3Fim, "%d/%m")," a ", format(semana3Inicio, "%d/%m")), paste(format(semana4Fim, "%d/%m")," a ", format(semana4Inicio, "%d/%m"))))
y
这些是警告:
Warning messages:
1: Removed 12 rows containing missing values (position_stack).
2: Removed 12 rows containing missing values (geom_bar).
3: Removed 12 rows containing missing values (geom_text).
我尝试过is.na(x),但是我想要一种特定的方法来查看缺失值在哪里。还尝试过position =“ dodge”,增加宽度,缩放连续范围,但没有任何效果。