0

In a plot of Y and X over categories Z, I would like for categories to be represented by points of different collor, except for one category, which I would like to be displayed as a line connecting the points.

Here is the data and what I have so far:

library(ggplot2);library(reshape);library(scales);library(directlabels)
dat <- read.csv("https://dl.dropboxusercontent.com/u/4329509/Fdat_graf.csv")
dat_long <- melt(dat, id="ano")
p <- qplot(ano,value, data=dat_graf_long, colour=variable)+   
scale_y_log10(breaks=c(.1,1,10,100,500,1000),labels = comma) + 
scale_x_continuous(breaks=seq(from=1960, to=2010, by=10))  + 
theme_bw()
direct.label(p)

I would like for the "Lei_de_Moore" category to be represented by a line, as in this example (done in Stata):

enter image description here

Also, I would like to change a few things (maybe I should ask tem in different topic?):

  • Change the style of the graph colors more "vivid", as in the Stata example
  • Change the Y aixis. I just want plain Numbers in non-scientific notation form. I used the labels="comma", but I don't want the coma itself. Ideally I would like the comma to be the decimal place separator.

EDIT: I had asked another question on how to embed the legend for this graph (this post: Legend as text alongside points for each category and with same collor)

Community
  • 1
  • 1
LucasMation
  • 2,408
  • 2
  • 22
  • 45
  • Did you delete your earlier identical question? (You should.) – IRTFM Sep 04 '14 at 20:00
  • Thanks, I edited that question (and this one) to avoid redundancy. I think as separate questions they will be more useful for others. Let me know if you and others still think that it should be deleted. – LucasMation Sep 04 '14 at 20:31

1 Answers1

0

You can mix geoms if you use ggplot and pass only a subset of the data to different geoms. Here you can pass everything in dat_long to geom_point except rows where variable is Lei_de_Moore, and then pass only those dat_long rows to geom_line in a different call.

p <- ggplot(dat_long, aes(ano, value, color=variable)) + 
    geom_point(data=dat_long[dat_long$variable != 'Lei_de_Moore',]) + 
    geom_line(data=dat_long[dat_long$variable == 'Lei_de_Moore',]) + 
    scale_y_log10(breaks=c(.1,1,10,100,500,1000),labels = comma) + 
    scale_x_continuous(breaks=seq(from=1960, to=2010, by=10))  + 
    theme_bw()

For colors, have a look at RColorBrewer package palettes. Install the package and use ?brewer.pal to see some more options. For example, this one might work:

p <- p + scale_color_brewer(palette="Set1")

For the y-axis labels, you'll probably have to hack something together. Have a look at this question. So you could do something like this:

fmt <- function(){
    f <- function(x) sub(".", ",", as.character(round(x,1)), fixed=T)
    f
}


p <- ggplot(dat_long, aes(ano, value, color=variable)) + 
    geom_point(data=dat_long[dat_long$variable != 'Lei_de_Moore',]) + 
    geom_line(data=dat_long[dat_long$variable == 'Lei_de_Moore',]) + 
    scale_y_log10(breaks=c(.1,1,10,100,500,1000), labels=fmt()) + 
    scale_x_continuous(breaks=seq(from=1960, to=2010, by=10))  + 
    theme_bw() +
    scale_color_brewer(palette="Set1")
Community
  • 1
  • 1
user2034412
  • 4,102
  • 2
  • 23
  • 22