0

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.

  1. How can I get rid of the warning:

convert_polygon_sf_to_polygon_df(): Not POLYGON or MULTIPOLYGON: c("XY", "LINESTRING", "sfg")

  1. 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.

  2. 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.

  3. 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())

ggplot

tonchy
  • 13
  • 1
  • Short information to the second question: before I created a variable age_groups with numbering 1, 2, 3, ... instead of the characters "18-29", "30-39", ... . And I would like to use the numbering and then change the names of the numbering in the plot. The same way you can do that in the barplot(..., names.arg = c(...)). Is there a way to do this? – tonchy Aug 18 '22 at 11:32
  • 1
    WRT point (3): change your manual fill scale to `scale_fill_manual(values=c(color[3], color[2]), labels = c("healed", "not healed"))`. – teunbrand Aug 18 '22 at 12:00

0 Answers0