1
votes

I have the following dataframe:

T<-matrix(0,11,3)
for(i in 1:length(T[,1])){
  T[i,1]<-rnorm(1)
  T[i,2]<-rnorm(1)
  T[i,3]<-rnorm(1)
}
T<-as.data.frame(T)
colnames(T)<-c('THBD','TLBD','C')

I want to plot each line separately on a plot, so I run the following

colors <- c("Control" = "blue", "Treat, Low" = "red", "Treat, High" = "black")
Q<-ggplot(T,aes(x=seq(-5,5)))+
  geom_line(aes(y=C,color="Control"))+
  geom_line(aes(y=TLBD,color="Treat, Low Bank Dependence"))+
  geom_line(aes(y=THBD,color="Treat, High Bank Dependence"))+
  labs(x='Years Around Merger',y='Log(Employment+1)',color="Legend")+
  scale_color_manual(values = colors)+
  scale_x_discrete(breaks=c('-5','-4','-3','-2','-1','0','1','2',
                            '3','4','5'),labels=c('-5','-4','-3','-2','-1','pig','1','2',
                                                  '3','4','5'))
Q

The graph that is generated is below:

enter image description here

As you can see the x-axis's label isn't changing, which I thought the bottom line of the ggplot code was supposed to do. I have tried editing the ggplot code so that the sequence for the x-axis is inside the T dataframe and I have tried to change the x-axis so that it is a character type instead of a numeric type, but neither of these strategies work. The difference between my code and most example codes on the internet that use scale_x_discrete to change x-axis tick labels seems to be that mine has multiple lines instead of just one, but I don't know how to fix this. How can I change the x-axis here?

1
You should avoid assigning anything to the constant T. You won't get an error, but in R T is an alias for TRUE.Charlie Gallagher

1 Answers

1
votes

The values you are passing to aes(x=) are continuous (numeric), not discrete (factor or character). Use scale_x_continuous

ggplot(T,aes(x=seq(-5,5)))+
  geom_line(aes(y=C,color="Control"))+
  geom_line(aes(y=TLBD,color="Treat, Low"))+
  geom_line(aes(y=THBD,color="Treat, High"))+
  labs(x='Years Around Merger',y='Log(Employment+1)',color="Legend")+
  scale_color_manual(values = colors) +
  scale_x_continuous(breaks=seq(-5,5),
                   labels=c('-5','-4','-3','-2','-1','pig','1','2',
                                                  '3','4','5'))