I wanted to create a graph to show which continent has the highest co2 emissions total per capita over the years, so I coded this:
barchart <- ggplot(data = worldContinents,
aes(x = Continent,
y = `Per.Capita`,
colour = Continent))
geom_bar(stat = "identity")
barchart
This is my dataframe:
Geometry
is just for some geographical mapping later.
I checked which(is.na(worldContinents$Per.Capita))
to see whether there were NA
values but it returned nothing
What's causing these gray lines?
How do I get rid of them?
These are the gray lines inside the bar graph
Thank you
CodePudding user response:
You have a couple of issues here. First of all, I'm guessing you want the fill
to be mapped to the continent, not color
, which only controls the color of the bars' outlines.
Secondly, there are multiple values for each continent in your data, so they are simply stacking on top of each other. This is the reason for the lines in your bars, and is probably not what you want. If you want the average value per capita in each continent, you either need to summarise your data beforehand or use stat_summary
like this:
barchart <- ggplot(data = worldContinents,
aes(x = Continent,
y = `Per.Capita`,
fill = Continent))
stat_summary(geom = "col", fun = mean, width = 0.7,
col = "gray50")
theme_light(base_size = 20)
scale_fill_brewer(palette = "Spectral")
barchart
Data used
Obviously, we don't have your data, so I used a modified version of the gapminder
data set to match your own data structure
worldContinents <- gapminder::gapminder
worldContinents$Per.Capita <- worldContinents$gdpPercap
worldContinents$Continent <- worldContinents$continent
worldContinents <- worldContinents[worldContinents$year == 2007,]