Columns are not in accordance to values.

I'm trying to create a columnn plot with 6 categories. But I'm not sure why when the graph produced, it got everything on the same level.


Here is the code I used:

barchart<-ggplot()
barchart<-barchart + geom_col(data=slowdown, aes(x=Period, y="Growth rate", fill=Measure), position="dodge")
barchart

Sorry I'm new to R :((

look at your y axis; your y quantity is the text "Growth Rate", to access a variable in the data.frame passed to ggplot() you normally just write it unqouted (like you did for period, where it has a space you cant do this, so instead you use backticks

y=`Growth rate`
1 Like

Thank you that's been solved. Also do you know how to add the title and also source and footnotes onto this graph?


I also just encountered a new problem after trying to fix it. Can someone help?

you wrote pivot longer statement with 2 opening brackets and only 1 closing bracket

( ( )


How about this time?? Thanks so much for helping btw

you cant do what you are trying to do (the message tells you why) , and I don't know why you are trying to do it ...

what are you hoping to achieve by pivoting longer that dataset with those columns ?

I followed a youtube video so that's why. Now I have deleted the pivoting longer. The graph was generated but it doesn't look as what I wanted. I want it to be categorized in time periods such as in the very first image of this post.

look at your x axis; your x quantity is the text "Time Period", to access a variable in the data.frame passed to ggplot() you normally just write it unqouted (like you did for period, where it has a space you cant do this, so instead you use backticks

x=`Time Period`

this is a repeat of your earlier issue with Growth Rate

Thanks alot. Can you also help with adding the second footnote/caption? I need another note under the Source but this one is quite a long sentence.

Im not sure what your problem is, perhaps you could say more about it.

example

library(tidyverse)

ggplot(data=data.frame(l=letters[1:2],
                       v=c(101,233))) +
  aes(x=l,y=v,fill=l) + 
  geom_col() +
  labs(caption="This is the first line of the caption\nthis is the second line of the caption")+theme(
         plot.caption=element_text(hjust = 0)
       )

Thank you that helped. Also, can you help me with adding a line timeline to the graph? Here is the example.

Here is my code:
barchart<-ggplot()
barchart<-barchart + geom_col(data=slowdown, aes(x=Time Period, y=Growth Rate, fill=Measure), position="dodge")
barchart<-barchart + labs(title="Soviet Economic Indicators, 1928-1985",
caption="Source: Ofer (1987, pp. 17/8-/9)\ To emphsize the long-run trends, the figures for the 1940s have been omitted. Growth rates in that decade were very low due to World War II.")+
theme(plot.caption = element_text(hjust=0))

line geometries add added by use of geom_line(), if you would learn and practice using ggplot2 the following are useful resources for you.

and secondly
The R Graph Gallery – Help and inspiration for R charts (r-graph-gallery.com)

Thanks for your help.
Another question, I now have to generate this type of graph which I have no idea how to. Do you know any resources would help or do you have any guidance?

its either a facet_wrap or a facet_grid; you can read about them

1 Like

I'm doing the facet_wrap now. I got it to 4 different grids but I want columns instead of lines. Can you show me how to do that? Here's my current graph:

My code:
ggplot(abc, aes(x=Year, y=GDP Value, group=Location))+ #
geom_col(aes(color=Location))+ #
geom_point(aes(color=Location))+
scale_y_continuous(labels=comma, breaks=seq())

Linechart+
facet_wrap(~Location)

you almost certainly made another chart with geom_line (Linechart) and are confusing that for the chart code that you wrote involving only geom_col and geom_point.

Can you help me to re-write the correct one??

if you run this and see an unfacetted chart that is otherwise what you want

ggplot(abc, aes(x=Year, y=GDP Value, group=Location))+ #
geom_col(aes(color=Location))+ #
geom_point(aes(color=Location))+
scale_y_continuous(labels=comma, breaks=seq())

then give either add the facet_wrap code onto the end of it, or give it a name and facet_wrap that name.
Linechart seems irrelevant as your chart code doesnt have an assignment to it .

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.