When i try to make a parallel coordinate in ggplot2, i get the message that it is deprecated:
require(ggplot2)
ggpcp(mtcars) + geom_line()
Warning message:
'ggpcp' is deprecated.
See help("Deprecated")
however, the ggplot documentation did not say anything about this: http://docs.ggplot2.org/current/ggpcp.html.
is there a new pcp function somewhere?
Migrated to GGally. E.g.:
require(GGally)
ggparcoord(mtcars, columns = c(1, 5:10)) + geom_line()
Related
I'm currently trying to create a dose-response curve with Rstudio and I'm using the tidydrc package. I was wondering if there was a way I could add minor gridlines, since the x-axis is log.
Here's an image of what I've got so far
This is the code I've got so far:
tidydrc_plot(Nifedipinedrc,ed50=FALSE) +
scale_x_log10() +
geom_errorbar(aes(ymin=g$Nifedipine-g$SD, ymax=g$Nifedipine+g$SD, x=Nifedipinedrc[[1]][[1]]$d, y=Nifedipinedrc[[1]][[1]]$r)) +
ggtitle("graph") +
labs(x="Concentration", y= "Force")
I know it's an absolute mess of a code but I'm completely self taught and I feel like I've hit a bit of a brick wall with this because I don't actually understand a lot of the guides currently on stack.
Here is a function that you can use for the minor_breaks argument of log scales. The proposed function uses an unexported function from ggplot2, so I'm cheating a little bit. This probably only works well for log10 axis though and you might want to accentuate the major gridlines some more through the theme() setting.
library(ggplot2)
# A function factory for minor log breaks
minor_breaks_log <- function(base) {
# Prevents lazy evaluation
force(base)
# Wrap calculation in a function that the outer function returns
function(limits) {
ggplot2:::calc_logticks(
base = base,
minpow = floor(log(limits[1], base = base)),
maxpow = ceiling(log(limits[2], base = base))
)$value
}
}
ggplot(msleep, aes(bodywt, brainwt)) +
geom_point() +
scale_y_log10(minor_breaks = minor_breaks_log(10)) +
scale_x_log10(minor_breaks = minor_breaks_log(10))
#> Warning: Removed 27 rows containing missing values (geom_point).
Created on 2020-12-02 by the reprex package (v0.3.0)
From library mgcv
i get the points to plot with:
fsb <- fs.boundary(r0=0.1, r=1.1, l=2173)
if with standard graphic package i plot fsb and then i add lines i get :
x11()
plot(fsb)
lines(fsb$x,fsb$y)
I try now with ggplot (this is the line within a bigger code) :
tpdf <- data.frame(ts=fsb$x,ps=fsb$y)
ts=fsb$x
ps=fsb$y
geom_line(data=tpdf, aes(ts,ps), inherit.aes = FALSE)
i get a messy plot:
I think that i'm failing the order in geom_line
This can be solved by using geom_path:
ggplot(tpdf)+
geom_point(aes(ts,ps)) +
geom_path(aes(ts,ps))
You have a very odd way of using ggplot I recommend you to reexamine it.
data:
library(mgcv)
fsb <- fs.boundary(r0 = 0.1, r=2, l=13)
tpdf <- data.frame(ts=fsb$x,ps=fsb$y)
You'll have to specify the group parameter - for example, this
ggplot(tpdf) +
geom_point(aes(ts, ps)) +
geom_line(aes(ts, ps, group = gl(4, 40)))
gives me a plot similar to the one in base R.
I need to repeat the thing done in:
tiny pie charts to represent each point in an scatterplot using ggplot2 but I stumbled into the problem that the package ggsubplot is not available for 3.3.1 R version.
Essentially I need a histogram or a pie chart in predefined points on the scatterplot. Here is the same code that is used in the cited post:
foo <- data.frame(X=runif(30),Y=runif(30),A=runif(30),B=runif(30),C=runif(30))
foo.m <- melt(foo, id.vars=c("X","Y"))
ggplot(foo.m, aes(X,Y))+geom_point()
ggplot(foo.m) +
geom_subplot2d(aes(x = X, y = Y, subplot = geom_bar(aes(variable,
value, fill = variable), stat = "identity")), width = rel(.5), ref = NULL)
The code used libraries reshape2, ggplot2 and ggsubplot.
The image that I want to see is in the post cited above
UPD: I downloaded the older versions of R (3.0.2 and 3.0.3) and checkpoint package, and used:
checkpoint("2014-09-18")
as was described in the comment bellow. But I get an error:
Using binwidth 0.0946
Using binwidth 0.0554
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
Which I can't get around, because when I try to include facet, the following error comes up:
Error: ggsubplots do not support facetting
It doesn't look like ggsubplot is going to fix itself any time soon. One option would be to use the checkpoint package, and essentially "reset" your copy of R to a time when the package was compatible. This post suggests using a time point of 2014-09-18.
I am writing a function that uses qplot() to draw a histogram, for example,
> library(ggplot2)
> d=rnorm(100)
> myfun=function(x) qplot(x)
Running it gives a warning:
> myfun(d)
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
To suppress the warning, I tried computing the binwidth myself, but this gives an error and doesn't plot:
> myfun=function(x) print(qplot(x, binwidth=diff(range(x))/30))
> myfun(d)
Error in diff(range(x)) : object 'x' not found
I have two related questions:
What is going on here? Why is object 'x' not found?
How can I write the function so the warning is not generated?
Thanks!
To attempt to clear up some confusion, this construct does not prevent the binwidth warnings/messages to appear:
suppressMessages(p <- ggplot(...))
print(p)
But this does:
p <- ggplot(...)
suppressMessages(print(p))
As Hadley's comment points out, lazy evaluation prevents the stat_* functions from actually running until they need to at print-time.
I can't explain the why of this one (Hadley may swing by and do so) but using ggplot instead of qplot solves the problem:
d <- data.frame(v1 = rnorm(100))
myfun <- function(x){
p <- ggplot(data = x, aes(x = v1)) +
geom_histogram(binwidth = diff(range(x$v1))/30)
print(p)
}
Doing it this way I get no warning message. Also, using ggplot and removing the binwidth = ... portion in geom_histogram makes the warning reappear, but then suppressMessages works as expected as well.
I suspect this has to do with namespaces or environments and when/where qplot and ggplot are evaluating arguments. But again, that's just a guess...
As they say on TV "Had this been a real warning you would have been given directions from your local authorities."
Since it wasn't a warning then my original answer didn't cause it to error out. This is what I should have written:
options(warnings= -1)
<do something> # no warnings
options(warnngs=1)
<business as usual>
But it wasn't a warning but a message to the console. Here's how to stop it:
con=file("temp.fil", "w")
sink(con, type="message")
library(ggplot2)
d=rnorm(100)
myfun=function(x) qplot(x)
myfun(d)
sink( type="message")
I started using the lattice graphic package but I stumbled into a problem. I hope somebody can help me out.
I want to plot a histogram using the corresponding function.
Here is the file foo.r:
library("lattice")
data <- data.frame(c(1:2),c(2:3))
colnames(data) <- c("RT", "Type")
pdf("/tmp/baz.pdf")
histogram( ~ RT | factor(Type), data = data)
dev.off()
When I run this code using R --vanilla < foo.r it works all fine.
However, if I use a second file bar.r with
source("bar")
and run R --vanilla < bar.r the code produces an erroneous pdf file.
Now I found out that source("bar", echo=TRUE) solves the problem. What is going on here? Is this a bug or am I missing something?
I'm using R version 2.13.1 (2011-07-08) with lattice_0.19-30
It is in the FAQ for R -- you need print() around the lattice function you call:
7.22 Why do lattice/trellis graphics not work?
The most likely reason is that you forgot to tell R to display the
graph. Lattice functions such as xyplot() create a graph object, but
do not display it (the same is true of ggplot2 graphics, and Trellis
graphics in S-Plus). The print() method for the graph object produces
the actual display. When you use these functions interactively at the
command line, the result is automatically printed, but in source() or
inside your own functions you will need an explicit print() statement.
Example of the case
visualise.r
calls plot2this.r
calls ggplot2 and returns p object
Here the fix in the function plot2this.r from return(p) to return(print(p)).
Initial plot2this.r
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(p)
Fix
p <- ggplot(dat.m, aes(x = Vars, y = value, fill=variable))
return(print(p))
Output now: expected output with the wanted plot.