confused about grouping points in ggplot2 - r

Why doesn't this code make lines between data at the same values of y?
main <- data_frame(x=rep(c(-1, 1), each=2), y = c(c(1, 1), c(2, 2)), z = c(1, 2, 3, 4))
qplot(data = main, x = x, y = z, geom="line", group=factor(y))
Here is what I get:
But I want only the points at the same level of y to be connected.

The issue is with how you defined your y variable. Change it to y = c(c(1,2), c(1,2)) and things should work.
Also, if you're going to use data_frame be sure to add the calls to library to make your code reproducible (i.e., library(dplyr) and library(ggplot2)).

Related

How to force the presentation of all y-axis value in R?

While plotting a boxplot in R, I noticed not all values in the y-axis are presented. Possible values are -5 to 5, but actual values are -1.3 to 4.6, so the values presented on the y-axis are -2 to 5. I want it to be presented with all values: -5 to 5, even though there's no data for this entire range.
My code looks like this:
boxplot(depvar ~ indepvar, data = a, pars = list(outlwd = 2, outcex = 1.8), axes = FALSE)
axis(side = 2, at = seq(-5, 5, by = 1), las = 1, tck = 7)
What should be added/changed for the y-axis to be fully-presented?
Appears simliar to this question: How to set the y range in boxplot graph?
I think you are looking for ylim.
a <- c((randu$x*3)-2)
boxplot(x = a,
ylim = c(-5,5))
Load Packages
install.packages("dplyr")
library(dplyr)
creating random set with two columns:
set.seed(10)
df <- dplyr::data_frame(
x = 1:5,
y = 1:5)
Visualize in a boxplot with expanded axis:
boxplot(x~y,
df,
xlim =c(-5,5),
ylim =c(-5,5))

Disable x-axis sorting in line chart

I have the following simple data.frame:
x <- data.frame(x = c(1, 3, 5, 2, 4, runif(10)),
y = c(1, 2, 3, 4, 5, runif(10)))
I want to make a plot showing both the scatter plot and connecting some of the points with a line, so I use:
plot_ly(data = x) %>%
add_markers(
x = ~x,
y = ~y
) %>%
add_lines(
x = ~x[1:5],
y = ~y[1:5]
)
However, the resulting line graph is sorted along the x-axis, while I want the line to follow the order found in the data.frame (shown in red below).
Is there any way of doing this? I've found similar questions on SO, but they all deal with categorical values.
I could obviously use paths, but to my understanding those only exist as shapes within layout(). I'm hoping for something which behaves like a trace: responds to hover actions, appears (and can be hidden) in the legend, etc.
I have just found a solution by using add_paths instead of add_lines.
plot_ly(data = x) %>%
add_markers(
x = ~x,
y = ~y
) %>%
add_paths(
x = ~x[1:5],
y = ~y[1:5]
)
Hope it solves your challenge.

How to color background in each panel of an xyplot according to a factor variable?

I'm trying to construct a xyplot which contains different background color according to different values of an additional categorical variable. It is no problem to get repeating background coloring with the panel.xblocks (package: latticeExtra) function, but up to now I found no method to implement this with different coloring for different subplots in the xyplot.
JD <- c(seq(0,19, 1), seq(0,19, 1))
VAR <- c(rnorm(20, mean=10, sd=1), rnorm(20, mean=10, sd=1))
CATEG <- c(rep("A", 5), rep("B", 15), rep("A", 10), rep("B", 10))
YEAR <- c(rep(2001, 20), rep(2002, 20))
myd <- data.frame(JD, VAR, CATEG, YEAR)
xyplot((VAR) ~ JD | factor(YEAR), type="l",
xlab="", ylab="", col=1, data=myd)+
layer_(panel.xblocks(x, CATEG,
col = c("lightgray")))
Running the above code, the background coloring from the first xyplot-subplot (year 2001) is repeated in the second xyplot-subplot (year 2002). my aim is to get different background coloring according to the varaiable "CATEG" for the two subplots. Any suggestions welcome.
I think the panel.xblocks function is a good approach. The use of subscripts and groups is handy too but always requires some re-learning for me.
The conditioning request (|) generates subscripts. The groups argument is used to pass the CATEG values to the panel function. It isn't actually used for any grouping here. The ... in the panel function is not actually used either, but it's a good practice in case the code is changed and other functions need arguments passed down.
# Starting with data in 'myd' from above
# Load non-standard packages
library(lattice)
library(latticeExtra)
# Old school colors
myCol <- c("salmon", "lightgray")
names(myCol) <- levels(myd$CATEG)
# To use a different color for each level of 'CATEG' in each panel:
obj1 <- xyplot(VAR ~ JD | factor(YEAR), data = myd,
groups = CATEG, xlab = "", ylab = "",
panel = function(x, y, subscripts, groups, ...) {
panel.xblocks(x, myCol[groups][subscripts])
panel.lines(x, y, col = 1)
})
# Here's a solution to a different problem (second plot):
# How to use a different color for the first level of 'CATEG' in each panel
obj2 <- xyplot(VAR ~ JD | factor(YEAR), data = myd,
xlab = "", ylab = "", groups = CATEG,
panel = function(x, y, subscripts, groups, ...) {
panel.xblocks(x, myCol[panel.number()][groups][subscripts])
panel.lines(x, y, col = 1)
})
# Plot in one device
plot(obj1, position = c(0, 0.45, 1, 1), more = TRUE)
plot(obj2, position = c(0, 0, 1, 0.55))

how to make a vertical line segment in plot?

I have a plot(x, y) and I want to add a vertical line at x = 2 ONLY from y = 1 to 4. I want to use the lines() function but I'm having trouble limiting the y-range.
What's an easy way to do this?
Here's a simple example of using plot and lines. To draw a line from (2, 1) to (2, 4), you have to provide the x coordinates and y coordinates as (2, 2) and (1, 4):
plot(1:5)
lines(c(2, 2), c(1, 4))
ggplot2 offers a very simple solution, too!
library(ggplot2)
set.seed(1)
# Create some dummy data
data.frame(X = rpois(n = 10, lambda = 3),
Y = rpois(n = 10, lambda = 2)) %>%
# Pipe to ggplot
ggplot(aes(X, Y)) +
geom_point() +
geom_segment(aes(x = 1, xend = 1, y = 1, yend = 4), color = "red")
Within the aesthetics call to geom_segment() you can select the start and end points for your x and y parameters. You can then easily add multiple segments by simply adding + geom_segment(aes(...)) to the end of the code above.
For completeness, there is also a base graphics function in R that will do this: segments(x0,y0,x1,y1):
plot(1:5)
segments(2,1,2,4)

cooks distance plot with R

Does anybody know, how to grab the single cooks distance plot that you get from this code:
treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2), labels = c("placebo","treated"))
improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)), levels = c(1, 2,3),labels = c("none", "some", "marked"))
numberofdrugs <- rpois(84, 5)+1
healthvalue <- rpois(84,5)
y <- data.frame(healthvalue, numberofdrugs, treatment, improved)
test <- glm(healthvalue~numberofdrugs+treatment+improved, y, family=poisson)
par(mfrow=c(2,2))
plot(test) # how to grab plot 2.1 ?
What I don't like to have is this
par(mfrow=c(1, 1))
plot(test, which=c(4))
because it doesn't have residuals on the y axis and leverage on the x axis!
Thanks guys
I'm not quite sure what your problem is. You seem to want the plot with residuals on the y axis and leverage on the x axis. Isn't that just the 5th (of 6) plot generated:
plot(test,which=5)
You can read more about this at ?plot.lm
Edit to address OP's question about setting y axis labels:
Usually, simply adding ylab="My Label" to the plot() call would work, but these graphs are designed to be produced "automatically" and so certain graphical parameters are 'hard coded'. If you pass your own ylab value, you'll get an error, as plot.lm() will be presented with two ylab's and won't know which one to use. If you really don't like the y axis label, your only option here is to grab the plot.lm code (just type 'plot.lm' at the console and hit enter) copy and paste it into a text file and look for this section:
if (show[5L]) {
ylab5 <- if (isGlm)
"Std. Pearson resid."
else "Standardized residuals"
r.w <- residuals(x, "pearson")
if (!is.null(w))
r.w <- r.w[wind]
rsp <- dropInf(r.w/(s * sqrt(1 - hii)), hii)
ylim <- range(rsp, na.rm = TRUE)
if (id.n > 0) {
ylim <- extendrange(r = ylim, f = 0.08)
show.rsp <- order(-cook)[iid]
}
and modify it with your own y axis label. Rename the function (say, plotLMCustomY, or something) and it should work.

Resources