How can I make the graph easier to read? Editing a plot

I would like to ask you how to modify my function plot

to make my graph more readable?

Here I will show you the code that I am using to build:

# open the pdf file
pdf(file='LSF1_PWD_GWD.pdf')
a <- c('LSF1', 'PWD', 'GWD')
rowsToPlot<-c(1066,2269,109)
matplot(as.matrix(t(tbl_alles[rowsToPlot,])),type=rep("l", length(rowsToPlot)), col=rainbow(length(rowsToPlot)),xlab = 'Fraction Size', ylab = 'Intensity')
legend('topright',a,lty=1, bty='n', cex=.75, col = rainbow(length(rowsToPlot)))
# close the pdf file
dev.off()

      

and what the graph looks like:

Graph

This is just a basic plot because I have no idea how to edit it. The arrow points three lines at one position, which you can't see because they overlap ... and that's the important part of this graph to me. Maybe I shouldn't use the dotted line? How can I change it?

Data:

tbl_alles <- 
  structure(list("10" = c(0, 0, 0, 0, 0, 0),
               "20" = c(0, 0, 0, 0, 0, 0),
               "52.5" = c(0, 0, 0, 0, 0, 0),
               "81" = c(0, 0, 1, 0, 0, 0),
               "110" = c(0, 0, 0, 0, 0, 0),
               "140.5" = c(0, 0, 0, 0, 0, 0),
               "189" = c(0, 0, 0, 0, 0, 0),
               "222.5" = c(0, 0, 0, 0, 0, 0 ),
               "278" = c(0, 0, 0, 0, 0, 0),
               "340" = c(0, 0, 0, 0, 0, 0),
               "397" = c(0, 1, 0, 0, 0, 0),
               "453.5" = c(0, 0.66069369, 0, 0, 0, 1),
               "529" = c(0, 0.521435654, 0, 0, 1, 0),
               "580" = c(0, 0.437291195, 0, 0, 1, 0),
               "630.5" = c(0, 0.52204783, 0, 0, 0, 0),
               "683.5" = c(0, 0.52429838, 0, 0, 0, 0),
               "735.5" = c(1, 0.3768651, 0, 1, 0, 0),
               "784" = c(0, 0, 0, 0, 0, 0),
               "832" = c(0, 0, 0, 0, 0, 0),
               "882.5" = c(0, 0, 0, 0, 0, 0),
               "926.5" = c(0, 0, 0, 0, 0, 0),
               "973" = c(0, 0, 0, 0, 0, 0),
               "1108" = c(0, 0, 0, 0, 0, 0),
               "1200" = c(0, 0, 0, 0, 0, 0)),
          .Names = c("10", "20", "52.5", "81",
                     "110", "140.5","189", "222.5",
                     "278", "340", "397", "453.5",
                     "529", "580", "630.5", "683.5",
                     "735.5", "784", "832", "882.5",
                     "926.5", "973", "1108", "1200"),
          row.names = c("at1g01050.1", "at1g01080.1",
                        "at1g01090.1","at1g01220.1",
                        "at1g01420.1", "at1g01470.1"),
          class = "data.frame")

      

RowsToPlot:

> dput(tbl_alles[rowsToPlot,])
structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 
0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 
0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 
0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 
0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 
0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826
), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0, 
0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0, 
0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81", 
"110", "140.5", "189", "222.5", "278", "340", "397", "453.5", 
"529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5", 
"926.5", "973", "1108", "1200"), row.names = c("at3g01510.1", 
"at5g26570.1", "at1g10760.1"), class = "data.frame")

      

+3


source to share


5 answers


Ok, here's a way to clearly distinguish between lines while keeping everything in the same plot. I use non-solid line types and different sizes to "make room" for the superimposed lines.

library(reshape2)
library(ggplot2)

dat <- as.data.frame(as.matrix(t(tbl_alles)))
dat$x <- as.numeric(row.names(dat))

ggplot(melt(dat, id.vars='x'),  aes(x=x, y=value, group=variable)) +
  geom_line(aes(color=variable, linetype=variable, size=variable)) +

  scale_linetype_manual(values=c('solid', 'dotted', 'dashed')) +
  scale_size_manual(values=c(1,3,1)) +
  scale_color_manual(values=c('black', 'red', 'white')) +

  theme(axis.text = element_text(color='black'),
        panel.background = element_rect('grey'),
        legend.key = element_rect('grey'),
        panel.grid = element_blank()) +

  labs(title='This is not a pretty chart, but you can make out the lines')

      

enter image description here



I took as a starting point your data from the dropdown column above:

tbl_alles <- structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0, 0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0, 0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81", "110", "140.5", "189", "222.5", "278", "340", "397", "453.5", "529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5", "926.5", "973", "1108", "1200"), row.names = c("at3g01510.1", "at5g26570.1", "at1g10760.1"), class = "data.frame")

      

+3


source


This is definitely not what you want, but maybe it could give you another idea.

X=structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 
0, 0), `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 
0, 0), `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 
0, 0), `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 
0, 0), `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 
0, 0), `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826
), `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), `882.5` = c(0, 
0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), `1108` = c(0, 
0, 0), `1200` = c(0, 0, 0)), .Names = c("10", "20", "52.5", "81", 
"110", "140.5", "189", "222.5", "278", "340", "397", "453.5", 
"529", "580", "630.5", "683.5", "735.5", "784", "832", "882.5", 
"926.5", "973", "1108", "1200"), row.names = c("at3g01510.1", 
"at5g26570.1", "at1g10760.1"), class = "data.frame");

library(ggplot2)
library(reshape2)
library(data.table)

X.dt<-as.data.table(t(X))
X.dt[,X:=1:dim(X.dt)[1]]
X.dt<-melt(X.dt, id='X')
ggplot(X.dt,aes(X, value,group=variable,color=variable))+
 geom_line()+
 facet_wrap(~variable, nrow=3)+
 guides(color=FALSE)+labs(x="X",y="Intensity")

      



enter image description here

+1


source


Since you have a discrete number of x values, I suggest using barplot instead. This will make the categories easier to distinguish and highlight the aspect of your interest.

First put the data in long format

dat <- structure(list(`10` = c(0, 0, 0), `20` = c(0, 0, 0), `52.5` = c(0, 0, 0), 
                 `81` = c(0, 0, 0), `110` = c(0, 0, 0), `140.5` = c(0, 0, 0), 
                 `189` = c(0, 0, 0), `222.5` = c(0, 0, 0), `278` = c(0, 0, 0), 
                 `340` = c(0, 0, 0), `397` = c(0, 0, 0), `453.5` = c(0, 0, 0), 
                 `529` = c(0, 0, 0), `580` = c(0, 0, 0), `630.5` = c(0, 0, 0), 
                 `683.5` = c(0, 0, 0.57073483), `735.5` = c(0, 1, 0.85691826), 
                 `784` = c(0, 0, 0.90706982), `832` = c(1, 1, 1), 
                 `882.5` = c(0, 0, 0), `926.5` = c(0, 0, 0), `973` = c(0, 0, 0), 
                 `1108` = c(0, 0, 0), `1200` = c(0, 0, 0)), 
                 .Names = c("10", "20", "52.5", "81", "110", "140.5", "189", 
                            "222.5", "278", "340", "397", "453.5", "529", "580", 
                            "630.5", "683.5", "735.5", "784", "832", "882.5", 
                            "926.5", "973", "1108", "1200"), 
             row.names = c("at3g01510.1", "at5g26570.1", "at1g10760.1"), 
             class = "data.frame")

library(tidyr)
dat$rowname <- rownames(dat)
ggdat <- gather(dat, key = "colname", value = "Intensity", -rowname)

      

Then create a tablet using ggplot2

library(RColorBrewer)
library(ggplot2)
colors <- brewer.pal(nrow(dat), "Dark2")
ggplot(data = ggdat, aes(x = colname, y = Intensity, fill = rowname)) +
    geom_bar(aes(color = rowname), stat = "identity", 
             position = position_dodge(), width = 0.75) +
    scale_fill_manual(values = colors) + 
    scale_color_manual(values = colors) +
    theme(axis.title.x = element_blank(),
          axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
          legend.position = "bottom")

      

enter image description here

The code can be used for more than three lines, although it will be more difficult for bars to distinguish more categories. If this is an issue, you might consider dropping / binning x values, or perhaps split the graph in two:

ggdat$group <- factor(ggdat$colname %in% colnames(dat)[1:12],
                      levels = c(TRUE, FALSE), labels = c("Low x", "High x"))
ggplot(data = ggdat, aes(x = colname, y = Intensity, fill = rowname)) +
    geom_bar(aes(color = rowname), stat = "identity", 
             position = position_dodge(), width = 0.75) +
    scale_fill_manual(values = colors) + 
    scale_color_manual(values = colors) +
    theme(axis.title.x = element_blank(),
          axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5),
          legend.position = "bottom") + 
    facet_wrap(~ group, ncol = 1, scales = "free_x")

      

enter image description here

+1


source


You can try to play with line types, but it can get very difficult if you have too many lines to see: is there a maximum you will have? Also, you may want to consider another way to render your data.

Here is an example with your data, when I draw it I see 3 lines:

matplot(as.matrix(t(tbl_alles[rowsToPlot,])),type="l",lwd=2,lty=c("solid","48","36"), col=rainbow(length(rowsToPlot)),xlab = 'Fraction Size', ylab = 'Intensity')
legend('topright',c('LSF1', 'PWD', 'GWD'),lty=c("solid","48","36"),lwd=2, bty='n', cex=.75, col = rainbow(length(rowsToPlot)))

      

3 types of lines:

solid

: this is the default type, as you already know ...

48

: first 4 units of a line, then a space of 8 units

36

: first 3 units of a line, then a space of 6 units.

I also changed the line width with lwd=2

.

There is one more parameter: transparency.

If (by keeping different lty's) you change the colors to c("#FF000030","#0000FF50","#00FF0080")

, for example, it will be easier to see each line (the last two characters of each hex code define transparency).

If you are using transparency then you can even specify a unique color and the ovelapping lines will appear darker: eg col=#00000044"

.

0


source


How many records does the dataset have? You seem to be dealing with a bulkhead problem. Follow @Nikos method to organize your data.

Use size

and alpha

to change the size and opacity of the line.

ggplot(data = X.dt, aes(x = X, y = value, group = variable, color = variable)) +
geom_line(data = X.dt, aes(x = X, y = value, group = variable, color = variable), 
size = 3, alpha = .25)

      

The line color changes as it matches. However, this will only work for small datasets. My only other suggestion is overlay geom_line()

on geom_point()

, which will draw dots over the lines. You can use position = position_jitter()

to slightly increase the position of the points so if they overlap you can see where they overlap.

ggplot(data = X.dt, aes(x = X, y = value, group = variable, color = variable)) +
geom_point(position = position_jitter(w = 0.001, h = 0.02), size = 3, alpha = .5) +
geom_line(data = X.dt, aes(x = X, y = value, group = variable, color = variable), size = 1, alpha = .25)

      

0


source







All Articles