I am trying to present some data
emertim=c("M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019")
group=c(rep("Bujqesi,Silvkulture dhe peshkim",6), rep("Industria",6), rep("Ndertimi",6), rep("Tregtia",6), rep("Informacioni dhe komunikimi",6), rep("Sigurimi",6), rep("Aktivitete legjislative dhe shkencore", 6), rep("Administrata publike dhe shendetesia",6), rep("Arte",6), rep(“Gjithsej”,6))
paga=c(37.163,34.305,37.529,35.486,37.753,35.069,49.301,31.916,51.541,33.454,53.759,35.172,44.057,37.903,44.457,39.737,42.284,41.997,36.044,32.224,38.303,34.330,40.651,36.495,78.427,66.893,81.683,68.162,85.468,73.232,116.081,95.406,121.384,96.997,118.629,100.775,57.191,53.961,60.625,56.816,61.143,56.992,64.869,55.969,66.108,57.149,68.404,59.081,47.647,52.078,49.608,54.202,59.760,55.653,51.392,46.018,53.181,47.490,54.940,49.411)
data=data.frame(emertim, group, paga)
Whatever I do, I get an error message "Error: Aesthetics must be either length 1 or the same as the data"
This is the code I have(too much code to be put in the question):
I can't seem to find the error.
Consider cleaning up your code of syntax issues and heed the error message that indicates the problematic issue (emphasis added):
Error: Aesthetics must be either length 1 or the same as the data (10): hjust
Also, use lapply
or for
to avoid the repetitive geom_segment
lines. Finally, consider saving angle
and hjust
aesthetics in base_data
and abbreviate the group names to better render on plot
Data
emertim <- c("M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018",
"F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019",
"M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018",
"F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019",
"M2017","F2017","M2018","F2018","M2019","F2019","M2017","F2017","M2018",
"F2018","M2019","F2019","M2017","F2017","M2018","F2018","M2019","F2019",
"M2017","F2017","M2018","F2018","M2019","F2019")
group <- c(rep("Bujqesi, Silvkulture dhe peshkim",6),
rep("Industria",6), rep("Ndertimi",6),
rep("Tregtia",6), rep("Informacioni dhe komunikimi",6),
rep("Sigurimi",6), rep("Aktivitete legjislative dhe shkencore", 6),
rep("Administrata publike dhe shendetesia",6), rep("Arte",6),
rep("Gjithsej",6))
paga <- c(37.163,34.305,37.529,35.486,37.753,35.069,49.301,31.916,51.541,
33.454,53.759,35.172,44.057,37.903,44.457,39.737,42.284,41.997,
36.044,32.224,38.303,34.330,40.651,36.495,78.427,66.893,81.683,
68.162,85.468,73.232,116.081,95.406,121.384,96.997,118.629,100.775,
57.191,53.961,60.625,56.816,61.143,56.992,64.869,55.969,
66.108,57.149,68.404,59.081,47.647,52.078,49.608,54.202,59.760,
55.653,51.392,46.018,53.181,47.490,54.940,49.411)
data <- data.frame(emertim, group, paga)
Graph Data
Note : Starting in R 4.0, data.frame
defaults with stringsAsFactors=FALSE
. So all string above are character
columns and not factor
. Therefore, factor()
below is used at nlevels
and levels
calls.
empty_bar <- 3
to_add <- data.frame( matrix(NA, empty_bar*nlevels(factor(data$group)), ncol(data)) )
colnames(to_add) <- colnames(data)
to_add$group <- rep(levels(factor(data$group)), each=empty_bar)
data <- rbind(data, to_add)
data <- data %>% arrange(group)
data$id <- seq(1, nrow(data))
label_data <- data
number_of_bar <- nrow(label_data)
angle <- 90 - 360 * (label_data$id-0.5) / number_of_bar
label_data$hjust <- ifelse(angle < -90, 1, 0)
label_data$angle <- ifelse(angle < -90, angle+180, angle)
base_data <- data %>%
# ABBREVIATE GROUP NAME
mutate(group = paste0(sapply(strsplit(group, " "), `[`, 1), "...")) %>%
group_by(group) %>%
summarize(start=min(id), end=max(id) - empty_bar) %>%
rowwise() %>%
mutate(title=mean(c(start, end)))
base_data <- base_data %>%
# ADD ANGLE AND HJUST COLUMNS
transform(angle = 90 - 360 * (seq(1, nrow(base_data))-0.5) / nrow(base_data)) %>%
mutate(angle = ifelse(angle < -90, angle+180, angle),
hjust = c(rep(1, 5), rep(0, 5)))
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,]
Plot
p <- ggplot(data, aes(x=as.factor(id), y=paga, fill=group)) +
geom_bar(aes(x=as.factor(id), y=paga, fill=group), stat="identity", alpha=0.5) +
lapply(seq(90, 0, by=-10), function(i)
geom_segment(data=grid_data, aes(x = end, y = i, xend = start, yend = i),
colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE)) +
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) +
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=paga+10, label=emertim, hjust=hjust),
color="black", fontface="bold", alpha=0.6, size=2.5,
angle=label_data$angle, inherit.aes = FALSE) +
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=group),
hjust=base_data$hjust, colour="black", alpha=0.8, size=4,
angle=base_data$angle, fontface="bold", inherit.aes=FALSE)
ggsave("myGroupCircularBarChart.png", plot=p, device="png")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.