Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Rui Barradas

Hello,

Here are two ways of drawing the lines black and at the same time 
removing the lines in the legend. The second way is more idiomatic.



1. Override the colour setting in the ggplot call when drawing the lines:

geom_line(aes(y = y1), colour = "black") +


2. Don't set the colour aesthetic in the initial ggplot call. It will be 
needed only to draw the points, so set it in geom_point(). Though it 
doesn't influence the lines' colour and the lines in the legend, the 
same principle applies to shape = variable so I have moved it to 
geom_point().


The complete instruction becomes:


ggplot(long, aes(x, y = value)) +
  geom_line(aes(y = y1)) +
  geom_point(aes(colour = variable, shape = variable)) +
  scale_colour_manual("Doesn't work",values=c("blue","red"),
  labels=c("clyde","irving")) +
  scale_shape_manual("Doesn't work",values=c(16,3),
 labels=c("clyde","irving")) +

  facet_grid(cols=vars(grp))



Hope this helps,

Rui Barradas


Às 01:07 de 02/12/19, Rolf Turner escreveu:


On 2/12/19 3:03 am, Rui Barradas wrote:


Hello,

See if this is it. The standard trick is to reshape the data from wide 
to long, see the SO post [1]. Then add a scale_shape_* layer to the plot.



yyy <- cbind(xxx, y3 = y3)
long <- reshape2::melt(yyy, id.vars = c("x", "y1", "grp"))

ggplot(long, aes(x, y = value, colour = variable, shape = variable)) +
   geom_line(aes(y = y1)) +
   geom_point() +
   scale_colour_manual("Doesn't work",values=c("blue","red"),
   labels=c("clyde","irving")) +
   scale_shape_manual("Doesn't work",values=c(16,3),
   labels=c("clyde","irving")) +

   facet_grid(cols=vars(grp))


[1] 
https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format 



Hope this helps,


Almost there.  However the colour of the line changes with "values" and 
lines show up in the legend.  I want the lines to be black in all 
facets, and only points to show up in the legend.


I fiddled about a bit trying to achieve this but only succeeded in 
messing things up completely.


Can you guide me a bit further, please?

cheers,

Rolf



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Rolf Turner

On 2/12/19 5:08 pm, Rui Barradas wrote:

Hello,

Here are two ways of drawing the lines black and at the same time 
removing the lines in the legend. The second way is more idiomatic.



1. Override the colour setting in the ggplot call when drawing the lines:

geom_line(aes(y = y1), colour = "black") +


2. Don't set the colour aesthetic in the initial ggplot call. It will be 
needed only to draw the points, so set it in geom_point(). Though it 
doesn't influence the lines' colour and the lines in the legend, the 
same principle applies to shape = variable so I have moved it to 
geom_point().


The complete instruction becomes:


ggplot(long, aes(x, y = value)) +
   geom_line(aes(y = y1)) +
   geom_point(aes(colour = variable, shape = variable)) +
   scale_colour_manual("Doesn't work",values=c("blue","red"),
   labels=c("clyde","irving")) +
   scale_shape_manual("Doesn't work",values=c(16,3),
  labels=c("clyde","irving")) +

   facet_grid(cols=vars(grp))



Hope this helps.


Boy did it *ever*!!!  Perfect. Thank you *HUGELY*!!!

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Still struggling with facet_grid_paginate() from package ggforce.

2019-12-01 Thread Rolf Turner



On 2/12/19 10:45 am, Rui Barradas wrote:


Hello,

Here are two ways.

The first is an adaptation from your code. It uses facet_wrap_paginate, 
not *_grid_*.



plotObj2 <- vector("list",2)
for(pg in 1:2) {
   plotObj2[[pg]] <- ggplot(egDat) +
     geom_point(aes(y = obsd, x = x),
    na.rm = TRUE, shape = 20, colour = "blue") +
     geom_line(aes(y = fit2, x = cPred)) +
     facet_wrap_paginate(facets = ~Trt,
     ncol = 4, nrow = 3, page = pg) +
     theme_bw()
}
print(plotObj2)


The second is an adaptation of SO[1]. It needs two calls to the plot 
code and it's slower but gets the job done.



g <- ggplot(egDat) +
   geom_point(aes(y = obsd, x = x),
  na.rm = TRUE, shape = 20, colour = "blue") +
   geom_line(aes(y = fit2, x = cPred)) +
   facet_wrap_paginate(facets = ~Trt, ncol = 4, nrow = 3, page = 1) +
   theme_bw()

n <- n_pages(g)
for(i in 1:n){
   print(g + facet_wrap_paginate(~Trt, ncol = 4, nrow = 3, page = i))
}

print(g)



Hope this helps.


Indeed it did.  You are brilliant, Rui. Problem solved.

I note that one can, I think, calculate the number of pages a priori,
thus avoiding two calls to ggplot(), in something like the following manner:

nface <- with(Dat,prod(sapply(condVars,
   function(x){length(levels(get(x)))})))

npgs  <- ceiling(nface/(nrow*ncol))

where Dat is the data frame of data being plotted and condVars is a 
vector of the names of the variables being conditioned on (i.e. the 
variables determining the facets).  In the example that I provided,
condVars would just be "Trt", but it all seems to work in settings in 
which there is more than one conditioning variable.


Thanks very much.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Rolf Turner



On 2/12/19 3:03 am, Rui Barradas wrote:


Hello,

See if this is it. The standard trick is to reshape the data from wide 
to long, see the SO post [1]. Then add a scale_shape_* layer to the plot.



yyy <- cbind(xxx, y3 = y3)
long <- reshape2::melt(yyy, id.vars = c("x", "y1", "grp"))

ggplot(long, aes(x, y = value, colour = variable, shape = variable)) +
   geom_line(aes(y = y1)) +
   geom_point() +
   scale_colour_manual("Doesn't work",values=c("blue","red"),
   labels=c("clyde","irving")) +
   scale_shape_manual("Doesn't work",values=c(16,3),
   labels=c("clyde","irving")) +

   facet_grid(cols=vars(grp))


[1] 
https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format 



Hope this helps,


Almost there.  However the colour of the line changes with "values" and 
lines show up in the legend.  I want the lines to be black in all 
facets, and only points to show up in the legend.


I fiddled about a bit trying to achieve this but only succeeded in 
messing things up completely.


Can you guide me a bit further, please?

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Still struggling with facet_grid_paginate() from package ggforce.

2019-12-01 Thread Rui Barradas

Hello,

Here are two ways.

The first is an adaptation from your code. It uses facet_wrap_paginate, 
not *_grid_*.



plotObj2 <- vector("list",2)
for(pg in 1:2) {
  plotObj2[[pg]] <- ggplot(egDat) +
geom_point(aes(y = obsd, x = x),
   na.rm = TRUE, shape = 20, colour = "blue") +
geom_line(aes(y = fit2, x = cPred)) +
facet_wrap_paginate(facets = ~Trt,
ncol = 4, nrow = 3, page = pg) +
theme_bw()
}
print(plotObj2)


The second is an adaptation of SO[1]. It needs two calls to the plot 
code and it's slower but gets the job done.



g <- ggplot(egDat) +
  geom_point(aes(y = obsd, x = x),
 na.rm = TRUE, shape = 20, colour = "blue") +
  geom_line(aes(y = fit2, x = cPred)) +
  facet_wrap_paginate(facets = ~Trt, ncol = 4, nrow = 3, page = 1) +
  theme_bw()

n <- n_pages(g)
for(i in 1:n){
  print(g + facet_wrap_paginate(~Trt, ncol = 4, nrow = 3, page = i))
}

print(g)



Hope this helps,

Rui Barradas


[1] https://stackoverflow.com/a/58373858/8245406


Às 11:46 de 01/12/19, Rolf Turner escreveu:


I am trying to produce a ggplot2 graphic in which there is a single 
conditioning variable with a large number of levels (24).


If I use facet_grid() I get a plot with either 24 rows or 24 columns,
both of which look like hell.

I thought that facet_grid_paginate() would rescue me, but it doesn't 
seem to.  I ask for 3 rows and 4 columns, and thought that I would get 
two 3 x 4 pages  Instead I get six pages with only one row (of four 
facets) per page.


Am I misunderstanding something?  Doing something silly?  Or is this a bug?

I have attached a reproducible example, along with the data set on which 
it depends.


Grateful for any insight.

cheers,

Rolf Turner


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How to use preProcess in Caret?

2019-12-01 Thread Burak Kaymakci
Hello there,

I am using caret and neuralnet to train a neural network to predict times
table. I am using 'backprop' algorithm for neuralnet to experiment and
learn.

Before using caret, I've trained a neuralnet without using caret, I've
normalized my input & outputs using preProcess with 'range' method. Then I
predicted my test set, did the multiplication and addition on predictions
to get the real values. It gave me good results.

What I want to ask is, when I try to train my network using caret, I get an
error saying algorithm did not converge. I am thinking that I might be
doing something wrong with my pre-processing,

How would I go about using preProcess in train?
Do I pass my not-normalized data set to the train function and train
function handles normalization internally?

You can find my R gist here


Thank you,
Burak
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Antony Unwin
How about defining your dataset differently, making the colouring property a 
variable?

xxx <- data.frame(x=rep(x, 4), y=c(y2, y3), grp=factor(rep(c("a","b"),each=20, 
times=2)), type=factor(rep(c("clyde", "irving"), each=40)))
ggplot(xxx, aes(x,y, colour=type, shape=type)) + geom_point() + 
geom_abline(intercept=3, slope=2) + facet_wrap(vars(grp)) + 
scale_colour_manual(values=c("blue", "red"))  + 
scale_shape_manual(values=c(20,3))

Then you could also plot the four groups separately if you wanted to:

ggplot(xxx, aes(x,y, colour=type, shape=type)) + geom_point() + 
geom_abline(intercept=3, slope=2) + facet_grid(rows=vars(type), cols=vars(grp)) 
+ scale_colour_manual(values=c("blue", "red"))  + 
scale_shape_manual(values=c(20,3))

Antony Unwin
University of Augsburg, 
Germany




> From: Rolf Turner 
> Subject: [R] Adding a legend to a (multi-facet) plot produced by ggplot().
> Date: 1 December 2019 at 01:04:46 CET
> To: R help 
> 
> 
> 
> I have been struggling to add a legend as indicated in the subject line,
> with no success at all.  I find the help to be completely bewildering.
> 
> I have attached the code of what I have tried in the context of a simple
> reproducible example.
> 
> I have also attached a pdf file of a plot produced with base graphics to 
> illustrate roughly what I am after.
> 
> I would be grateful if someone could point me in the right direction.
> 
> cheers,
> 
> Rolf Turner
> 
> -- 
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Jeffrey Pullin
Hi Rolf,

Some code to produce the plot you want is here:

https://gist.github.com/jeffreypullin/be752f11a136601ffecddc73ba0519b9

Hope you find it helpful.

Personally I have found that the key to effective ggplot2 use is getting
your data into the right format (one data.frame, tidy style) before you plot
it.

Cheers
Jeffrey

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding a legend to a (multi-facet) plot produced by ggplot().

2019-12-01 Thread Rui Barradas

Hello,

See if this is it. The standard trick is to reshape the data from wide 
to long, see the SO post [1]. Then add a scale_shape_* layer to the plot.



yyy <- cbind(xxx, y3 = y3)
long <- reshape2::melt(yyy, id.vars = c("x", "y1", "grp"))

ggplot(long, aes(x, y = value, colour = variable, shape = variable)) +
  geom_line(aes(y = y1)) +
  geom_point() +
  scale_colour_manual("Doesn't work",values=c("blue","red"),
  labels=c("clyde","irving")) +
  scale_shape_manual("Doesn't work",values=c(16,3),
  labels=c("clyde","irving")) +

  facet_grid(cols=vars(grp))


[1] 
https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format


Hope this helps,

Rui Barradas

Às 00:04 de 01/12/19, Rolf Turner escreveu:


I have been struggling to add a legend as indicated in the subject line,
with no success at all.  I find the help to be completely bewildering.

I have attached the code of what I have tried in the context of a simple
reproducible example.

I have also attached a pdf file of a plot produced with base graphics to 
illustrate roughly what I am after.


I would be grateful if someone could point me in the right direction.

cheers,

Rolf Turner


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.