1

I have a dataset with the following variables: iso3 (country code), region, crude rate, age-standardized rate. Rates can be positive, negative or be equal to 0. Because the values are skewed towards one end for both crude and age-standardized rates, values need to be transformed.

I would like to do a scatter plot of transformed values of crude and age-standardized rates but keeping the x and y axis labels in real state space, using ggplot2. The dots would be colored depending on the region variable.

I have tried this:

pdf("Graphs/Scatter_Crude_ASDR.pdf")
scatter <- ggplot(df_scatter, aes(x = crude, y = agestd, color = region, label=iso3)) +
  geom_point(size = 2) +
  scale_x_continuous(trans=sign(crude) * log(abs(crude) + 1)) +
  scale_y_continuous(trans=sign(agestd) * log(abs(agestd) + 1)) +
  geom_text(hjust=-0.2, vjust=0, size=3) +
  geom_abline() +
  geom_hline(yintercept = 0) +
  labs(
    x="Crude rate",y="Age-standardized rate") +
  theme(
    plot.title = element_text(color = "#0099f9", size = 20, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 13, face = "bold", hjust = 0.5),
    plot.caption = element_text(face = "italic", hjust = 0)
  ) +
  guides(colour = guide_legend(title = "region"))
print(scatter)
dev.off()

R studio returns:

Error in is.trans(x) : object 'crude' not found

Here is a sample of my dataset:

# A tibble: 10 × 4
   iso3  region     crude         agestd
   <chr> <chr>      <dbl>          <dbl>
 1 AFG   EMRO        67.5         198.  
 2 AGO   AFRO        18.3          72.9 
 3 ALB   EURO       255.          134.  
 4 AND   EURO        93.9          55.1 
 5 ARE   EMRO        21.4          55.7 
 6 ARG   AMRO       139.          101.  
 7 ARM   EURO       354.          251.  
 8 ATG   AMRO       -38.8         -32.9 
 9 AUS   WPRO        12.2           1.22
10 AUT   EURO        98.9          36.6 
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Nelly
  • 11
  • 2
  • Please provide a reproducible example, you can try `head(df_scatter, 10)` and put the console output in your question. However, the `scales` functions can't recognize the columns of your data frames as an object (if you have an object with the same name, take this as argument in function). You need to change it with `df_scatter$crude` & `df_scatter$agestd` in `scale_x_continuous` & `scale_y_continuous`. – Reza Esmaeelzade Mar 31 '23 at 08:20
  • Still the same issue, even if I change to df_scatter$crude and df_scatter$agestd. I have added a sample of my dataset but do not really know how to format it for stack overflow. Sorry! – Nelly Mar 31 '23 at 09:09
  • @Nelly Please read [this](https://stackoverflow.com/q/5963269/8449629) for how to make a reproducible example in SO. Axis transformations in ggplot are not the simplest things to get right, & your odds of getting help will be greater if people have a better idea of the value ranges in your actual dataset. – Z.Lin Mar 31 '23 at 09:47
  • You need to pass a **trans** object to `trans` in **scales** functions. you can use `trans_new()` to create a new transformation. – Reza Esmaeelzade Mar 31 '23 at 14:48

0 Answers0