0

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

5
  • In aes, x and y are set to a string variables (x="Segment 1"), not a numeric value. You are attempting to plot strings as points. Commented Feb 17, 2016 at 17:19
  • How should I do it ? Generaly when I use string variable in only one axis, it doesn't complain. I though that in data_melt the fact that they were on the same line was enough Commented Feb 17, 2016 at 17:34
  • 3
    You have forgot a +. VTC as "simple typographical error". Commented Feb 17, 2016 at 17:36
  • So on the x-axis you want to plot a factor essentially. For the y-axis, what are you trying to capture ? If you are trying to capture the variable number, you will have to just have the values 1 or 2 in the variable column. Commented Feb 17, 2016 at 17:40
  • I updated my question to be clearer. What I would like is that the column value would define the size of each circle. Commented Feb 17, 2016 at 17:52

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Ok... I think I am a bit ashamed of this one... Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.