Yohan Obadia
Yohan Obadia

Reputation: 2672

R and ggplot2 : plot with factors on both axis

My data frame looks like this :

data
>
          | Segment 1 | Segment 2  | Segment 3
Segment 1 | 0.6861964 | 0.16669839 | 0.14710523
Segment 2 | 0.2211293 | 0.69263231 | 0.08623838
Segment 3 | 0.2595055 | 0.05354549 | 0.68694899

I would like to use in both axis Segment 1, Segment 2 and Segment 3 and use the values inside the table as parameters to have points of different sizes.

What I tried for the moment is to add a column Segments to the above data frame and did a melt() in order to obtain the following table:

data_melt <- melt(data, id = "Segments")
>
  | Segments  | variable  | value
1 | Segment 1 | Segment 1 | 0.68619638
2 | Segment 2 | Segment 1 | 0.22112931
3 | Segment 3 | Segment 1 | 0.25950552
4 | Segment 1 | Segment 2 | 0.16669839
5 | Segment 2 | Segment 2 | 0.69263231
6 | Segment 3 | Segment 2 | 0.05354549

Then trying to plot it I am stuck and could not find a ressource solving my problem.

ggplot(test, aes(x = Segments, y = variable, size = value))
    geom_point()

It plots the axis but no point appears and I get the following message:

geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity 

I would like the output to look like this: enter image description here

Upvotes: 0

Views: 3083

Answers (1)

Manfred Mustermann
Manfred Mustermann

Reputation: 36

I was able to reproduce by

data_melt <-  data.frame(seg=c(1,2,3,1,2,3),var=c(1,1,1,2,2,2),value=runif(6))

> ggplot(test, aes(x = Seg, y = var, size = value))
Error: No layers in plot
> geom_point()
geom_point: na.rm = FALSE 
stat_identity:  
position_identity: (width = NULL, height = NULL)

basically you forgot a +

 ggplot(data_melt, aes(x = seg, y = var,size=value))+geom_point()

Upvotes: 2

Related Questions