Example:
library(xts)
data(sample_matrix)
matrix_xts <- as.xts(sample_matrix, dateFormat='Date')
matrix_xts[,1] = matrix_xts[,1] * 100
plot(matrix_xts)
If i plot this it will be hard to visualize the data. Is it possible somehow to have the first column be plotted on the secondary y -axis in xts.plot?
To my knowledge this isn't supported with xts plotting. (Also some people do not favour plotting with 2 different axes on a single plot for a variety of reasons such as it could be seen as misleading.)
Here is one work around. Plot the columns with different scales on different plots:
# Get latest version of xts for nice new plot tools (>= 0.10 not yet on CRAN)
library(devtools)
install_github("joshuaulrich/xts")
plot(matrix_xts[, 2:4])
lines(matrix_xts[,1], on = NA)
?plot.xts for xts version >=0.10 has some helpful examples for options related to the plot like colours, etc.
Related
I am ploting a multiple histogram for 5 variables, and I have the same title repeated along all of them. I didn´t find a way to personalize it for each histogram. Is there a way to do it? I am using the psych R package.
Maybe another package to recommend?
The code line:
multi.hist(AutosCompleteNorm[,11:15],main="bah")
And "bah" is repeated 5 times. I tried c("a","b",..."e") as an argument but it didn´t work.
Package documentation:
https://www.rdocumentation.org/packages/psych/versions/1.8.3.3/topics/multi.hist
Thanks!
The current psych package doesn't seem to support this, which is strange because it would have been a natural feature for such a plot.
For customizability, I recommend you to take a look at ggplot and then layout in the format you wish using gridExtra.
Here's the code to create the few histogram in ggplot:
library(ggplot2)
p1 <- ggplot(vids, aes(x=log(likes)))+geom_histogram()+labs(title="title1")
p2 <- ggplot(vids, aes(x=log(dislikes)))+geom_histogram()+labs(title="title2")
p3 <- ggplot(vids,
aes(x=log(comment_count)))+geom_histogram()+labs(title="title3")
And then laying them up in a 2 row layout (nrow=2):
library(gridExtra)
grid.arrange(p1, p2, p3, nrow = 2)
Changing the layout to nrow=1:
Following these comments, I just updated psych so that multi.hist is more useful.
You can now specify the margins for the plots, and it will, by default label each plot with the variable name.
I have not pushed the development version of psych (1.8.9) to CRAN yet, but it is available on my repository at
install.packages("psych",repos="https://personality-project.org/r",type="source")
I hava a raster with 10 values:
vals <- c(1,2,3,4,5,12,123,1234,12345,23,234,2345,34,345,45)
I would like to produce a plot, where value 1 to 5 is coloured. And all other values are in grey scale. Plus, I'd like the legend to specifically show each value and the corresponding color. In ArcGIS this is easy to accomplish (see desired outcome)
I tried a lot, but I could not come up with a solution to get a good result. Can anyone help?
Raster can be found here: https://www.dropbox.com/s/kzcba9r4t9z4mc1/raster.tif?dl=0
Edit:
Turns out, the base plotting is a bit annoying with categorical data. I'm sure there's a way, but why take the rocky road if there's a highway.
The great package rasterVis makes it easy to plot categorical rasters ... and many more things for that matter. Check out the homepage for all the functionality.
Back to your problem:
library(raster)
library(rasterVis)
#load raster
r <- raster('raster.tif')
#define color palette
cpal <- c('darkred','orange','green','dodgerblue','navy',grey.colors(8,start = 0.4,end=0))
#make categorical
r2 <- ratify(r)
#plot
levelplot(r2,col.regions=cpal,att='ID')
And the result:
I have 23 different groups,each of them consists of from 7 to 20 individual samples (totally approximately 350-400 observations) with their own x,y & z coordinates. I'd like to produce 3D plot based on the data i have by means of plot3d function of rgl R package. It's not a big deal in general. The problem, that i'd like to make each one from the mentioned above 23 groups to be easy distinguishable on the 3D plot. I tried to use different colors for each group, but unfortunately it's not possible to find a 23 well recognizable by human eyes colors. I was thinking about pch parameter like in the plot function of base R library. But, again, as i can see there is not such option in the plot3d function. Besides, i have to explain, that there are too much points in my data set and adding the labels to each point (e.g. with text3d rgl function) is not a good idea (they will overlap with each other and give in result some kind of a mess on the 3D plot). Is there way to figure out it (i gues it's very common problem)? Thank you in advance!
Below is code of some toy example for better explanation:
# generate data
prefix=rep("ID",69)
suffix=rep(1:23,3)
suffix_2=as.character(suffix[order(suffix)])
titles_1=paste(prefix,suffix,sep="_")
titles_2=titles_1[order(titles_1)]
x=1:69
y=x+20
z=x+50
df=data.frame(titles_2,x,y,z)
# load rgl library
library('rgl')
# make 3D plot
plot3d(x,y,z)
If you like living on the bleeding edge, there's a new function rgl::pch3d() that draws symbols using the same codes as points() does
in base graphics. It's in rgl 0.95.1475, available on R-forge (and within a few hours on Github; see How do I install the latest version of rgl?). It's not completely working with rglwidget() yet.
The example code
open3d()
i <- 0:25; x <- i %% 5; y <- rep(0, 26); z <- i %/% 5
pch3d(x, y, z, pch = i, bg = "green")
text3d(x, y, z + 0.3, i)
pch3d(x + 5, y, z, pch = LETTERS[i+1])
text3d(x + 5, y, z + 0.3, i+65)
produces this display (after some resizing and rotation):
It's not perfect, but how about using letters a-w to distinguish the groups?
with(df,plot3d(x,y,z))
with(df,text3d(x,y,z,texts=letters[titles_2]))
Because i'm going to use the 3D plot for publication purposes i used this solution for now. It's not pretended to be the best one.
# generate data
prefix=rep("ID",69)
suffix=rep(1:23,3)
suffix_2=as.character(suffix[order(suffix)])
titles_1=paste(prefix,suffix,sep="_")
titles_2=titles_1[order(titles_1)]
x=1:69
y=x+20
z=x+50
df=data.frame(titles_2,x,y,z)
# load rgl library
library('rgl')
# load randomcoloR library
library(randomcoloR)
# create a custom palette
palette <- distinctColorPalette(23)
palette(palette)
# make 3D plot
plot3d(x,y,z,size = 10,col=suffix[order(suffix)])
I have merged two xts objects and want to plot them in a single display. This works fine when I use points (type="p"). However, when I use lines (type="l") a problem occurs: the first series is shown only in the index region that is not covered by the second series. I would expect the lines to be as long as the "points". A reproducible example is posted below.
As this occurs with both the default and the ggplot plotting commands, I suspect that this relates to some property of time-series data.
What is the reason for this behaviour? Is there a proper way of plotting this kind of data?
## Minimal example for Reproduction
library(xts)
library(ggplot)
# create two artificial xts objects
xts1 <- xts(1:15,Sys.Date()+10+seq(from=1,by=5,length.out=15))
xts2 <- xts(1:20,Sys.Date()+seq(from=1,by=2,length.out=20))
# merge them
merged.xts <- merge.xts(xts1,xts2)
# Plot as zoo objects to allow for panels
# plotting with points shows both series
plot(as.zoo(merged.xts),type="p",plot.type="single")
# plotting with lines
# The second series is "shortened"
plot(as.zoo(merged.xts),type="l",plot.type="single")
# Similar behaviour with ggplot2
autoplot(merged.xts)
Quite simply, type="l" looks the way it does because you can't plot a line on a single point. Set type="b" to see both lines and points.
I have multiple time series objects covering different periods of time, with gaps in the data, and with varying frequencies (some hourly data, some 15-minute, some 1-minute).
I'm trying to plot different time series objects against one another in x-y scatterplots - to see if there are obvious correlations, and make 'pretty' plots with ggplot for presentation. Obviously, one can only plot/compare data where it exists for x and y at the same time.
I'm able to get a quick graphic with base graphics, but would like something more presentable.
for example, this works in base R:
require(zoo)
x <- zoo(sort(rnorm(10)),as.POSIXct("2013/07/26")+1:10)
y <- zoo(sort(rnorm(30)),as.POSIXct("2013/07/26")+(1:30)/2)
plot(x,y)
and trying to do the same with ggplot fails:
data <- rbind(melt(fortify(x),id="Index"),melt(fortify(y),id="Index"))
ggplot(data,aes(x=x,y=y))+geom_point()
Don't know how to automatically pick scale for object of type zoo. Defaulting to continuous
Don't know how to automatically pick scale for object of type zoo. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:x
suggestions on better titles/description are welcome
What about this:
aaa<-merge(x,y, all=FALSE)
ggplot(aaa,aes(x=x,y=y))+geom_point() ?