display a secondary x axis category

Ciao guys,

I have the following dataframe

obj <- data.frame (percentile = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6 , 0.7, 0.8, 0.9, 1),
                   emp_change = c(0.05, 0.04, 0.03, 0.05, 0.06, 0.04, 0.02, 0.09, 0.08, 0.06), 
                   task = c("Manual", "Manual", "Manual", "Routine-Manual", "Routine-Manual",
                            "Routine-Abstract", "Routine-Abstract", "Abstract", "Abstract", "Abstract"))

My goal is to display the variable task on my x axis as well. I don't know if it would be necessary to generate a second x axis. I know that the question is pretty straightforward and was definitely asked before but I fail as I only found solutions for barplots.

This is my plot. Note also, that I use in my real data geom_smooth rather than geom_line. But I guess this should be irrespective when creating a second x axis.

plot <- ggplot() +
  geom_line(data = obj, aes(x= percentile, y= emp_change, group = 1, color="observed", linetype = "observed"), 
      size=1.5, linetype = "dotdash")
print(plot +  theme(axis.text.x=element_text(angle = 60, hjust = 1)) + 
        theme(axis.title=element_text(size=12)) +
        labs(y="100 x Change in Employment Share", x = "Percentile ranked by task input and log of mean occupational wage (1990)"))

Many thanks in advance

Freddy

I suggest you try a couple of things other than a second x axis: colored points with the color encoding the task or geom_text to label the points. I have rough versions of each below. For the colored points, I would probably also change the line to a simple line instead of dashed. My geom_text version is rather ugly but you could adjust the text placement, even making y a constant value.

library(ggplot2)
obj <- data.frame (percentile = c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6 , 0.7, 0.8, 0.9, 1),
                   emp_change = c(0.05, 0.04, 0.03, 0.05, 0.06, 0.04, 0.02, 0.09, 0.08, 0.06), 
                   task = c("Manual", "Manual", "Manual", "Routine-Manual", "Routine-Manual",
                            "Routine-Abstract", "Routine-Abstract", "Abstract", "Abstract", "Abstract"))

#colored points
plot <- ggplot() +
  geom_line(data = obj, 
            aes(x= percentile, y= emp_change, group = 1, ), 
            size=1.5, linetype = "dotdash")+
  geom_point(data = obj, 
            aes(x= percentile, y= emp_change,color=task),size=2)
print(plot +  theme(axis.text.x=element_text(angle = 60, hjust = 1)) + 
        theme(axis.title=element_text(size=12)) +
        labs(y="100 x Change in Employment Share", x = "Percentile ranked by task input and log of mean occupational wage (1990)"))


#geo_text
plot <- ggplot() +
  geom_line(data = obj, 
            aes(x= percentile, y= emp_change, group = 1, ), 
            size=1.5)+
  geom_text(data = obj, 
             aes(x= percentile, y= emp_change+0.005,label=task),vjust=0)
print(plot +  theme(axis.text.x=element_text(angle = 60, hjust = 1)) + 
        theme(axis.title=element_text(size=12)) +
        labs(y="100 x Change in Employment Share", x = "Percentile ranked by task input and log of mean occupational wage (1990)"))

Created on 2021-05-15 by the reprex package (v0.3.0)

1 Like

thanks for your response! But the problem is that my real data is based on geom_smooth, so a local regression. In addition, there are 300 observations, for which reason the ideas above would not be as descriptive.
Thanks for your help anyways

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.