heino06 - 1 year ago 46

R Question

I tried to create a ggplot based on this data:

Sorry, but i can't provide more from the data.

I got this from an output of effect from the effects package.

Now i want to draw a ggplot that combines the probabilities from prob.X1 to prob.X6. It should look like this:

That's what i already tried:

`Eff.1 <- Effect(focal.predictors = "AvRating", mod= V6)`

Eff.df <- data.frame((Eff.1))

This code provides the chart from the top.

With this i tried to get the ggplot:

`ggplot(Eff.df) + geom_area(aes(x=AvRating, y=prob.X1))`

But this creates just the plot out of the first and the second column.

I tried to add more geoms like this (that doesn't work):

`ggplot(Eff.df) + geom_area(aes(x=AvRating, y=prob.X1)) + geom_area(aes(x=AvRating, y=prob.X2))`

Than i tried to connect the columns and plot this(that doesn't work):

`Eff.dfx <- as.numeric((rbind(Eff.df$prob.X1,Eff.df$prob.X2, Eff.df$prob.X3, Eff.df$prob.X4,Eff.df$prob.X5,Eff.df$prob.X6)))`

Eff.dfAv <- as.numeric(rep(Eff.df$AvRating,6))

ggplot(Eff.df) + geom_area(aes(x=Eff.dfAv, y= Eff.dfx))

Would you help me with the code of this ggplot?

Thanks a lot.

Answer Source

It always helps if you actually provide a MWE or data that people can use to actually help you. Since you didn't, here is some data already in the format that you probably need. You will need `reshape2::melt`

to convert your data (which is currently a picture, which can't have been easier than posting data) to the proper format.

You probably shouldn't use connections between the ratings, since that implies that you have information about values halfway between them

```
library(dplyr)
usableData <-
data.frame(
avgRating = factor(rep(0:5, each = 6))
, value = factor(rep(1:6, 6))
, count = rnorm(36, rep(seq(300,40, length.out = 6)), 10)
) %>%
group_by(avgRating) %>%
mutate(prob = count / sum(count))
ggplot(usableData
, aes(x = avgRating
, y = prob
, fill = value)) +
geom_bar(stat = "identity") +
theme_minimal() +
scale_fill_brewer(direction = -1)
```

If you really, really have a good reason for using the connected lines, you should be able to do something like this:

```
usableData %>%
ungroup() %>%
group_by(avgRating) %>%
mutate(cumsum = cumsum(count)
, cumProb = cumsum / sum(count)
, cumProbPre = c(0,cumsum[-length(cumsum)]) / sum(count)
) %>%
ggplot(aes(x = as.numeric(avgRating)
, ymin = cumProbPre
, ymax = cumProb
, fill = value
)) +
geom_ribbon() +
theme_minimal() +
scale_fill_brewer(direction = -1)
```