-1

I would like to plot my figure using R (ggplot2). I'd like to have a line graph like image 2.

here my.data:

B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,gen,xaxile
0.3795,0.4192,0.4675,0.5357,0.6217,T18-Yield,B50K
0.3178,0.3758,0.4249,0.5010,0.5870,T20-Yield,B50K+1000C50K
0.2795,0.3266,0.3763,0.4636,0.5583,T21-Yield,B50K+2000C50K
0.2417,0.2599,0.2898,0.3291,0.3736,T18-Fertility,B50K+4000C50K
0.2002,0.2287,0.2531,0.2962,0.3485,T19-Fertility,B50K+8000C50K
0.1642,0.1911,0.2151,0.2544,0.2951,T20-Fertility

***--> The delimiter is ",". By the way, I have not any useful .r script which would be helpful or useful.

The illustrated image shows my figure in Microsoft word.

enter image description here

I have tried several scripts via internet but non of them have not worked.

would you please help me to have a .r script to read my data file like img1 and plot my data like illustrated figure.

  • 1
    is there any chance that you could edit your question to paste your data file as *text* rather than as an image link? or even better, post a full reproducible example? – Ben Bolker Dec 27 '14 at 14:24
  • One way to share your data, is by using `dput`. To give the first 20 rows of your data you would post the results of `dput(yourdata[1:20, ])`. It would be good if you could also share the code you have tried to create the plot. Thanks – user20650 Dec 27 '14 at 14:28
  • Dear Ben and user20650, my post was edited by importing my data and substituted tab delimiter with ",". Any help would be appreciated. – user1677032 Dec 27 '14 at 19:17
  • Why does the last row of your data contain one element less than the other rows? – Sven Hohenstein Dec 27 '14 at 20:33
  • because the elements of sixth column are five. In fact, sixth column is the x axis in the figure. Except header, first 3 rows are the solid lines (and in the R graph should be solid), and rows 4, 5 and 6 are the dashed lines in the above figure (and in the R hraph should be dashed). – user1677032 Dec 27 '14 at 21:48
  • @user1677032; ass Sven has pointed out your data format is a little confusing (mainly the `xaxile` column)- have you managed to read the data into R? If so can you please post the output of `dput(yourdata)` (where `yourdata` is the name of your dataframe / data). Thanks. – user20650 Dec 27 '14 at 22:27
  • Thanks - the solution doesn't really change as `xaxile` is not used. The only change you will need is in the `read.table` arguments, you will need to add `fill=TRUE`. Have a look at `?read.table` to see what this does. – user20650 Dec 27 '14 at 23:07

1 Answers1

1

The trick is to reshape your data (using melt from the reshape2 package) so that you can easily map colours and linetypes to gen.

# Your data - note i also added an extra comma after the fifth column in row 6.
# It would be easier if you gave data using dput as described in comments above - thanks
dat <- read.table(text="B50K,B50K+1000C50K,B50K+2000C50K,B50K+4000C50K,B50K+8000C50K,xaxile,gen
0.3795,0.4192,0.4675,0.5357,0.6217,B50K,T18-Yield
0.3178,0.3758,0.4249,0.5010,0.5870,B50K+1000C50K,T20-Yield
0.2795,0.3266,0.3763,0.4636,0.5583,B50K+2000C50K,T21-Yield
0.2417,0.2599,0.2898,0.3291,0.3736,B50K+4000C50K,T18-Fertility
0.2002,0.2287,0.2531,0.2962,0.3485,B50K+8000C50K,T19-Fertility
0.1642,0.1911,0.2151,0.2544,0.2951,,T20-Fertility", 
header=T, sep=",", na.strings="")

# load the pckages you need
library(ggplot2)
library(reshape2)

# assume xaxile column is unneeded? - did you add this column yourself? 
dat$xaxile <- NULL

# reshape data for plotting
dat.m <- melt(dat)

# plot
ggplot(dat.m, aes(x=variable, y=value, colour=gen, 
                  shape=gen, linetype=gen, group=gen)) +
                  geom_point() + 
                  geom_line() 

You can then use scale_linetype_manual and scale_shape_manual to manually specify how you want the plot to look. This post will help, but there are many others as well

Community
  • 1
  • 1
user20650
  • 24,654
  • 5
  • 56
  • 91
  • Thanks user 20650. It is great and you save the day! It is exactly like the figure which I wished. A question: In the figure the order of "gen" elements is not as the data set. Could we keep it as the data set in the figure? – user1677032 Dec 27 '14 at 23:30
  • 1
    Youre welcome. Have a look at the ordering of the `factor` levels of `gen`; `levels(dat.m$gen)`. You change the order using the `?factor` command ie before plotting do `dat.m$gen <- factor(dat.m$gen, levels=unique(dat$gen), labels=unique(dat$gen))` to get the order that they appear in your original data. (Note you could type the levels and labels out manually - which i would do as it is a bit safer and more transparent) – user20650 Dec 27 '14 at 23:59
  • 2
    @user1677032 If the answer worked for you, it would be appreciated if you accept the answer. This will give future readers a clue about the value of the solution. See also this help page: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jaap Dec 28 '14 at 09:48
  • Thanks again, but based on written above script: How can I remove legend title? and how to keep $xaxile in the x axix as it is. Because, for instance, in the figure B50K+1000C50K is shown as B50K.1000C50K. In fact "+" has been changed to dot. In addition, in dput(dat) command "+" sign is being changed to ".". – user1677032 Dec 28 '14 at 21:48
  • 1
    (1) add `scale_shape_discrete(name="")+ scale_colour_discrete(name="")+ scale_linetype_discrete(name="")` (or use `name=""` in any `scale_colour_manual()` (etc.) specifications you may choose) there *may* be a less clunky way to do this) (2) specify `check.names=FALSE` when you use `read.table()` or `read.csv()` to import the data into R. – Ben Bolker Dec 28 '14 at 23:05
  • @user20650: better solution than mine! – Ben Bolker Dec 28 '14 at 23:07
  • In addition to Ben's answer, for further use of ggplot you may find this site useful http://www.cookbook-r.com/Graphs/ – user20650 Dec 28 '14 at 23:07
  • When I change fonts of the x and y axis values, the values lost their right position on x and y axis. They move a bit far away their exact positions! Would you address me the right command to reset them in their positions? – user1677032 Dec 29 '14 at 23:48
  • geom_point(size=4) + geom_line(lwd=1.2) + theme(axis.text.x = element_text(angle = 00, hjust = 1, size=11, color="darkred")) + theme(axis.text.y = element_text(angle = 90, vjust = 1, size=13, color="darkred")) + theme(axis.title.y = element_text(size = rel(1.4), angle = 90)) + theme(axis.title.x = element_text(size = rel(1.2), angle = 00)) + theme(legend.text=element_text(size=14)) – user1677032 Dec 30 '14 at 07:45
  • you can also use base R function `stack` instead of `melt` – ClementWalter Jul 06 '16 at 21:08
  • @clemlaflemme; can you show how please. `stack(dat)` doesn't retain the `gen` column – user20650 Jul 06 '16 at 21:26
  • sure you have to add it manually `res <- stack(dat); res$gen <- rep(dat$gen, 5)`. This was just a comment because some people cannot install the packages they want to. I thought this can help – ClementWalter Jul 06 '16 at 22:15
  • ah okay, thanks, you need to do it manually. Then I think `melt` is probably a bit safer in this case. But for another base r way, `reshape(dat, idvar=c("xaxile", "gen"), varying=1:5, direction="long")` – user20650 Jul 06 '16 at 22:45