0

I am trying to rename the columns headings for a facet_wrap. Instead of "False" and "True" I need them to say "1946-1992" and "1993-" respectively. Thank you!

library(tidyverse)

IdealPoint <- IdealpointestimatesAll_Apr2020 %>% #
  select(ccode, session, IdealPoint) %>% 
  mutate(year = session + 1945)

CINC <- NMC_5_0 %>% 
  select(stateabb, ccode, year, cinc)

Merged <- CINC %>% 
  group_by(stateabb, year) %>% 
  filter(year >= 1946) %>% 
  full_join(IdealPoint, by = c("ccode" = "ccode", "year" = "year"))

ggplot(data=Merged, 
       aes(x= log(cinc),y= IdealPoint, color = ccode,
           )) + geom_point() +
  facet_wrap(~year >=1993) +
  ylab("Ideal UN Voting Point") +
  xlab("National Material Capability") +
  ggtitle("National Material Capability Vs. State Positions on US-Led Liberal Order") +
  theme_bw()

Figure 1

NelCap
  • 27
  • 4
  • 1
    Does this answer your question? [How to change facet labels?](https://stackoverflow.com/questions/3472980/how-to-change-facet-labels) – Dylan_Gomes Oct 08 '20 at 23:05

2 Answers2

1

Try creating the variable for facets directly in dplyr pipeline:

#Adjust code
Merged <- CINC %>% 
  group_by(stateabb, year) %>% 
  filter(year >= 1946) %>% 
  full_join(IdealPoint, by = c("ccode" = "ccode", "year" = "year")) %>%
  mutate(NewVar=ifelse(year >=1993,"1993-Today","1946-1992"))
#Plot
ggplot(data=Merged, 
       aes(x= log(cinc),y= IdealPoint, color = ccode,
       )) + geom_point() +
  facet_wrap(~NewVar) +
  ylab("Ideal UN Voting Point") +
  xlab("National Material Capability") +
  ggtitle("National Material Capability Vs. State Positions on US-Led Liberal Order") +
  theme_bw()
Duck
  • 39,058
  • 13
  • 42
  • 84
1

You can use the labeller option:

library(ggplot2)
ggplot(data=iris, 
   aes(x= Sepal.Length,y= Sepal.Width
   )) + geom_point() +
facet_wrap(~Species) +
theme_bw()

enter image description here

rename <- c(`setosa` = "1946-1992",`versicolor` = "1993-",`virginica`="1996")

ggplot(data=iris, 
       aes(x= Sepal.Length,y= Sepal.Width
       )) + geom_point() +
     facet_wrap(~Species, labeller = as_labeller(rename)) +
    theme_bw()

enter image description here

Using your example, this should work:

rename <- c(`FALSE` = "1946-1992",`TRUE` = "1993-")

ggplot(data=Merged, 
       aes(x= log(cinc),y= IdealPoint, color = ccode,
           )) + geom_point() +
  facet_wrap(~year >=1993, labeller=as_labeller(rename)) +
  ylab("Ideal UN Voting Point") +
  xlab("National Material Capability") +
  ggtitle("National Material Capability Vs. State Positions on US-Led Liberal Order") +
  theme_bw()
Dylan_Gomes
  • 2,066
  • 14
  • 29