I used the code in a prior stackoverflow post, which previously made me the graph i wanted, with a legend, however now i am using the exact same code yet i am not getting a legend on my barplot.
dput(year.dat2)
structure(list(year = structure(c(1136044800, 1167577200, 1199113200,
1230735600, 1262275200, 1136044800, 1167577200, 1199113200, 1230735600,
1262275200, 1136044800, 1167577200, 1199113200, 1230735600, 1262275200
), class = c("POSIXct", "POSIXt"), tzone = ""), variable = structure(c(1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("SM1",
"SM2", "SM3"), class = "factor"), value = c(1.24863586758821,
1.23185757914453, 1.10997352162401, 1.13683917747257, 0.987520605867152,
2.21498726809749, 1.6378992693761, 1.25635623380691, 1.13585705516765,
1.10169569342842, 7.40955802109858, 5.7940698875978, 6.03438772314438,
6.82271157830123, 7.24402375195127)), row.names = c(NA, -15L), .Names =
c("year",
"variable", "value"), class = "data.frame")
ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
geom_bar(stat = "identity", position = "dodge")+
scale_fill_discrete(name = "variable",
breaks = c(1, 2, 3),
labels = c("SM1", "SM2", "SM3")) +
xlab("year") +
ylab("yearly Sub Mean")
Resulting plot:
Remove breaks
in scale_fill_discrete
, they do not correspond to values of your factored variable
data used as fill aes
.
This is the code you want:
ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_discrete(name = "variable",
labels = c("SM1", "SM2", "SM3")) +
xlab("year") +
ylab("yearly Sub Mean")
Note 1: You do not even need the labels
parameter as you are not renaming the variable
categories. scale_fill_discrete(name = "variable")
would be sufficient or labs(fill="variable")
is all you need to change the legend title.
Note 2: In your original post you linked to this SO question: How to get a barplot with several variables side by side grouped by a factor
Here, the breaks
in the sample code scale_fill_discrete(name="Gender", breaks=c(1, 2), labels=c("Male", "Female"))
actually references the values of gender
in the original df. Meanwhile, labels
was used to rename 1
to Male
and 2
to Female
in the legend.
Group.1 tea coke beer water gender
1 1 87.70171 27.24834 24.27099 37.24007 1
2 2 24.73330 25.27344 25.64657 24.34669 2
On the other hand, there are no 1,2,3
values in your variable
data that would match the breaks
you have set in your original code, and that is why your legend is not plotted.
And for fun, here is an example how you might use breaks
and labels
in your data set:
ggplot(year.dat2, aes(x = year, y = value, fill = factor(variable))) +
geom_bar(stat = "identity", position = "dodge")+
scale_fill_discrete(name = "variable",
breaks = c("SM2", "SM3", "SM1"),
labels = c("SM2 new label", "SM3 new label", "SM1 new label")) +
xlab("year") +
ylab("yearly Sub Mean")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.