1

I have a CSV which has following data:

 Hybrid    ON   OFF Model
1  1.022 1.033 0.939   283
2  0.988 1.016 1.068   283
3  1.012 0.958 1.872   283
4  1.073 5.476 0.907   283
5  1.054 0.952 0.902   283
6  0.992 0.941 0.908   283

I am trying to create a dot plot with something like this image.

enter image description here

Basically red green and blue are hybrid, on and off respectively, and on x-axis they are grouped on the 'Model'. I am familiar with creating simple plots using plot() but I have been reading some tutorial that this could be achieved using ggplot and melt..which looks a bit advanced. Appreciate if someone can provide some indicators. I have tried to create levels for different config after loading the csv:

load <- read.csv("combined_temp.csv", sep="," , header=TRUE)    

df <- data.frame(Config= rep(c("Hybrid", "ON", "OFF")))

I want use ggplot after this but not sure how to do that.. even not sure if I am doing the replication right. Sorry I am extremely new to R.

2
  • For some inspiration on a compareble problem, see this answer Commented Oct 15, 2015 at 12:19
  • I am sure there is a much simpler way to solve this. Anyone? Commented Oct 15, 2015 at 22:36

1 Answer 1

0

The first step is to reshape your data into long format, after that you can make such a plot easily with ggplot2:

library(reshape2)
dfl <- melt(df, id = "Model")

library(ggplot2)
ggplot(dfl, aes(x = factor(Model), y = value)) +
  geom_point(aes(color = variable), shape = 1, size = 4, position = position_dodge(width = 0.4)) +
  theme_bw()

this results in the following plot:

enter image description here


For illustrative reasons, I have expanded and changed the data.

Used data:

df <- structure(list(Hybrid = c(1.022, 0.988, 1.012, 1.073, 1.054, 0.992, 2.022, 1.988, 2.012, 2.073, 2.054, 1.992), 
                     ON = c(1.033, 1.016, 0.958, 3.476, 0.952, 0.941, 2.033, 2.016, 1.958, 3.476, 1.952, 1.941),
                     OFF = c(0.939, 1.068, 1.872, 0.907, 0.902, 0.908, 2.939, 1.068, 1.872, 2.907, 2.902, 2.908),
                     Model = c(283L, 283L, 283L, 283L, 283L, 283L, 382L, 382L, 382L, 382L, 382L, 382L)),
                .Names = c("Hybrid", "ON", "OFF", "Model"), class = "data.frame", row.names = c(NA, -12L))
Sign up to request clarification or add additional context in comments.

2 Comments

This looks good, thanks. Why do we need to use position_dodge? I took a similar approach in my solution but used facet to group the plot based on Model. Since using facet achieves the same purpose, what is the benefit of using position_dodge vs facet?
The benefit of using position_dodge is that you can plot the different models in one plot. However, when you have a lot of Models, it's indeed better to use facets.

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.