Reputation: 483
This is my data frame:
df1<-structure(list(Country_name = c("Austria", "Austria", "Austria",
"Austria", "Belgium", "Belgium", "Belgium", "Belgium", "Cyprus",
"Cyprus", "Cyprus", "Cyprus", "Denmark", "Denmark", "Denmark",
"Denmark", "Finland", "Finland", "Finland", "Finland", "France",
"France", "France", "France", "Germany", "Germany", "Germany",
"Germany", "Greece", "Greece", "Greece", "Greece", "Iceland",
"Iceland", "Iceland", "Iceland", "Ireland", "Ireland", "Ireland",
"Ireland", "Italy", "Italy", "Italy", "Italy", "Luxembourg",
"Luxembourg", "Luxembourg", "Luxembourg", "Malta", "Malta", "Malta",
"Malta", "Netherlands", "Netherlands", "Netherlands", "Netherlands",
"North Cyprus", "North Cyprus", "North Cyprus", "North Cyprus",
"Norway", "Norway", "Norway", "Norway", "Portugal", "Portugal",
"Portugal", "Portugal", "Spain", "Spain", "Spain", "Spain", "Sweden",
"Sweden", "Sweden", "Sweden", "Switzerland", "Switzerland", "Switzerland",
"Switzerland", "United Kingdom", "United Kingdom", "United Kingdom",
"United Kingdom"), Regional_indicator = c("Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe", "Western Europe", "Western Europe",
"Western Europe", "Western Europe"), continent = c("Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe", "Europe"),
variables_names = c("lowerwhisker", "Ladder_score", "upperwhisker",
"Logged_GDP_per_capita", "lowerwhisker", "Ladder_score",
"upperwhisker", "Logged_GDP_per_capita", "lowerwhisker",
"Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita",
"lowerwhisker", "Ladder_score", "upperwhisker", "Logged_GDP_per_capita"
), variables_values = c(7.22880411148071, 7.29419994354248,
7.35959577560425, 10.742823600769, 6.79613399505615, 6.86350011825562,
6.93086624145508, 10.6736392974854, 6.05981206893921, 6.15899991989136,
6.25818777084351, 10.4057025909424, 7.57995510101318, 7.64559984207153,
7.71124458312988, 10.774001121521, 7.747633934021, 7.80870008468628,
7.86976623535156, 10.6392669677734, 6.59014940261841, 6.66379976272583,
6.73745012283325, 10.5842227935791, 7.00600481033325, 7.0757999420166,
7.14559507369995, 10.7328186035156, 5.42301368713379, 5.5149998664856,
5.6069860458374, 10.1323261260986, 7.38765287399292, 7.50449991226196,
7.62134695053101, 10.7725591659546, 7.01556777954102, 7.09369993209839,
7.17183208465576, 11.1609783172607, 6.30255556106567, 6.38740015029907,
6.47224473953247, 10.4818363189697, 7.17703056335449, 7.23750019073486,
7.29796981811523, 11.4506807327271, 6.68860197067261, 6.77279996871948,
6.85699796676636, 10.5338382720947, 7.39442825317383, 7.44890022277832,
7.50337219238281, 10.8127117156982, 5.43546724319458, 5.53550004959106,
5.63553285598755, 10.4057025909424, 7.41971874237061, 7.48799991607666,
7.55628108978271, 11.0878038406372, 5.8067102432251, 5.9109001159668,
6.0150899887085, 10.2637424468994, 6.31799125671387, 6.40089988708496,
6.48380851745605, 10.462926864624, 7.28248071670532, 7.35349988937378,
7.42451906204224, 10.7587938308716, 7.49127197265625, 7.55989980697632,
7.62852764129639, 10.9799327850342, 7.09166288375854, 7.16450023651123,
7.23733758926392, 10.6001348495483)), row.names = c(NA, -84L
), class = c("tbl_df", "tbl", "data.frame"))
When I change the colors of the stacked bar the colors are changed. But the problem is when I set new names for my labels the legend is duplicated.
Why this is happening?
This is my code:
ggplot(df1) +
geom_bar(aes(x = variables_values, y = Country_name,
group = Country_name, linetype = variables_names,
fill = variables_names),
stat = 'identity',
lwd = 0.71,
width = 0.5,
position = 'stack') +
scale_fill_manual(values = c('red','blue','green','red'), labels = letters[1:4])
Upvotes: 0
Views: 417
Reputation: 1945
For each aes
other than x
and y
adds another legend
accordingly, if you specify aes(..., linetype = var, fill = var)
you will get one legend
per level of the specified variables.
Therefore, as also mentioned in the comments, you will need to keep only a single aes
, unless you are working with high dimensional data.
ggplot(df1) +
geom_bar(
aes(x = variables_values, y = Country_name,
fill = variables_names),
stat = 'identity',
lwd = 0.71,
width = 0.5,
position = 'stack'
) +
scale_fill_manual(values = c('red', 'blue', 'green', 'red'),
labels = letters[1:4])
Upvotes: 1