I looked these problems up on the internet, but I couldn't manage to solve the problems by myself. That's why I'm asking for help now.
- How can I get rid of the warning:
convert_polygon_sf_to_polygon_df(): Not POLYGON or MULTIPOLYGON: c("XY", "LINESTRING", "sfg")
I want to change the labels/names of the x-axis without changing my data coding. I used ifelse and a new variable before plotting, but this doesn't feel right.
Also I want to change the labels/names of the legend to "healed" and "not healed". I tried to use scale_fill_discrete() but the program said that I shouldn't use two fill functions in one.
In the beginning I tried to remove the NAs of the plot. I tried everything from this site Eliminating NAs from a ggplot but I couldn't make it work. That's why I changed my data before plotting. But I would like to do this without creating new data frame.
This is the code:
psa_data_rep <- psa_data[!is.na(psa_data$healing), ]
age_groups_rep <-
ifelse(psa_data_rep$age < 30, "18-29",
ifelse(psa_data_rep$age < 40, "30-39",
ifelse(psa_data_rep$age < 50, "40-49",
ifelse(psa_data_rep$age < 60, "50-59",
ifelse(psa_data_rep$age < 70, "60-69",
ifelse(psa_data_rep$age < 80, "70-79",
ifelse(psa_data_rep$age < 90, "80-89", "90-99")))))))
ggplot(data = psa_data_rep,
aes(x = age_groups_rep,
fill = factor(healing),
label = age_groups_rep)) +
geom_bar_pattern(stat = "count",
position = "fill",
pattern_color = "black",
pattern_fill = "white",
aes(pattern = factor(healing)),
colour="black") +
xlab("age groups") +
ylab("relative number of patients") +
ggtitle("healing outcome distribution") +
theme_bw()+
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black"),
legend.title = element_blank(),
axis.line.x = element_blank(),
axis.ticks.x.bottom = element_blank()) +
scale_fill_manual(values=c(color[3], color[2])) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1)),
labels = scales::percent_format())