Reputation: 21
I recently updated R and ggplot2 to the versions (4.1.1 and 3.3.5 respectively). I was using this code to create plots in ggplot:
rejA<-c(0.05,0.10,0.00,0.05)
Tr<-c("Corn\nmale\nx\nCorn\nfemale","Corn\nmale\nx\nWheat\nfemale","Wheat\nmale\nx\nCorn\nfemale","Wheat\nmale\nx\nWheat\nfemale")
Matingdiet<-c("Same diet","Different diets","Different diets", "Same diet")
d<-as.data.frame(cbind(Tr,rejA,Matingdiet))
d$rejA<-as.numeric(as.character(d$rejA))
d$Matingdiet<-factor(d$Matingdiet,levels = rev(levels(d$Matingdiet)))
mycol<-RColorBrewer::brewer.pal(9,"YlOrBr")[c(7,4)]
dev.new()
pl<-ggplot(data = d, aes(x=Tr, y=rejA))+
geom_col(aes( fill=Matingdiet))+
labs(x = "Mating pairs",fill="Male and female from", y = "Proportion of mates rejected")+
scale_y_continuous(limits=c(0,1),expand = c(0,0))+
scale_fill_manual(values=mycol)+
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.text=element_text(size = 10),
strip.background = element_blank(),
axis.text.y=element_text(size = 10),
panel.border=element_blank(),
panel.spacing.x = unit(1,"line"),
strip.text.x = element_text(size = 20))+
geom_hline(yintercept = 0)
pl
However, the colors are not showing up in the graph. I used the same code earlier to get this plot
However, now when I run the same code, all the bars are grey and there is no legend.
I have tried changing the colours in the code
pl<-ggplot(data = d, aes(x=Tr, y=rejA))+
geom_col(aes( fill=Matingdiet))+
labs(x = "Mating pairs",fill="Male and female from", y = "Proportion of mates rejected")+
scale_y_continuous(limits=c(0,1),expand = c(0,0))+
scale_fill_manual(values=c("red","blue"))+
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line.y = element_line(colour = "black"),
axis.text=element_text(size = 10),
strip.background = element_blank(),
axis.text.y=element_text(size = 10),
panel.border=element_blank(),
panel.spacing.x = unit(1,"line"),
strip.text.x = element_text(size = 20))+
geom_hline(yintercept = 0)
pl
This also return a graph with all bars in grey. I am not sure what to try now, especially since it was working earlier (in R version 3.5).
Upvotes: 2
Views: 1009
Reputation: 15123
I'm not sure but in line
d$Matingdiet<-factor(d$Matingdiet,levels = rev(levels(d$Matingdiet)))
change data d
from
Tr rejA Matingdiet
1 Corn\nmale\nx\nCorn\nfemale 0.05 Same diet
2 Corn\nmale\nx\nWheat\nfemale 0.10 Different diets
3 Wheat\nmale\nx\nCorn\nfemale 0.00 Different diets
4 Wheat\nmale\nx\nWheat\nfemale 0.05 Same diet
to
Tr rejA Matingdiet
1 Corn\nmale\nx\nCorn\nfemale 0.05 <NA>
2 Corn\nmale\nx\nWheat\nfemale 0.10 <NA>
3 Wheat\nmale\nx\nCorn\nfemale 0.00 <NA>
4 Wheat\nmale\nx\nWheat\nfemale 0.05 <NA>
You're filling chart with NA
s. Problem is by levels(d$Matingdiet)
, that Matingdiet
is not yet an factor so it does not have any levels
. I'm not sure about your purpose, but try add d$Matingdiet <- factor(d$Matingdiet)
first then d$Matingdiet<-factor(d$Matingdiet,levels = rev(levels(d$Matingdiet)))
will not cause any problems.
Upvotes: 1