0

I am a beginner in r studio but i want to create a diagram for a term paper. I work with data from the European Social Survey(ESS) Round 9, which is freely available. http://nesstar.ess.nsd.uib.no/webview/index.jsp?v=2&submode=abstract&study=http%3A%2F%2F129.177.90.83%3A80%2Fobj%2FfStudy%2FESS8e02.2&mode=documentation&top=yes

data <- read.dta("ESS8.dta")
data_de <- data[(data$cntry=="DE"),]

I want to create a bar chart that shows the political activity of people (y axis) broken down on the levels of a scale from Political orientation (0=Left, 10 = Right).

For the political activity i want to use multiple items. This are the items I want to use:

data_de$pbldmn
data_de$badge
data_de$bctprd
data_de$contplt
data_de$pstplonl
data_de$sgnptit
data_de$wrkprty
data_de$wrkorg

all items are factor variables. The values look like this:

table(data_de$sgnptit) Yes No Refusal Don't know No answer 336 2516 0 0 0

They should be stacked on each other in the bar chart, but only with the answers "Yes". I know that you can use group = for this but the problem is that I don't have one variable with several groups. I only have multiple different variables.

I tried this code but it didn't worked

PolAkt <- rep(c(data_de$pbldmn, data_de$sgnptit, etc....)

ggplot(data = data_de, aes(x=data_de$lrscale, y=stat(count), group=factor(PolAkt)))
+geom_bar()

Error: Aesthetics must be either length 1 or the same as the data (2852): group 

is there a another way to define these items as levels of a new variable?

I did a quick sketch how I would like the chart to look like, so you can maybe understand better what I want to do...

the political activity should be displayed proportional to the frequencies of the values of the x axis.

For example: The bar at lrscale 0 should show, how many % of the people who classified themselves on 0 on the scale from left(0) to right(10) are politically active and in which way.

enter image description here

would be very happy about any help!

EDIT

tried this:

data_PolAkt <- data.frame(PolAkt=c(data_de$lrscale, data_de$pbldmn, data_de$badge, data_de$bctprd, data_de$contplt, data_de$pstplonl, data_de$sgnptit, data_de$wrkprty, data_de$wrkorg))
ggplot(data_de, aes(x = data_de$lrscale, fill = PolAkt) ) +  
    geom_bar( aes(y = ..count..*100/sum(..count..) ) ) +
    xlab("EinordnungLinksRechts") +
    ylab("PolitischeAktivität") + 
    scale_fill_manual("Condition", values = alpha( c("firebrick", "dodgerblue4"), 1) ) 

and got this error: Error: Aesthetics must be either length 1 or the same as the data (2852): fill

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
leak
  • 1
  • 1
  • 3
    Does this answer your question? [Stacked bar chart in R (ggplot2) with y axis and bars as percentage of counts](https://stackoverflow.com/questions/3619067/stacked-bar-chart-in-r-ggplot2-with-y-axis-and-bars-as-percentage-of-counts) – neuroandstats Mar 28 '21 at 20:07
  • 1
    To download the data requires login. Can you provide data or a subset of data in a reproducible format using `dput` ? Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Mar 29 '21 at 04:32

0 Answers0