I have one data set which contain data in two column about Gross salary and tax wedge.You can see data with code line below:
SAMPLE_WAGES_TAX_WEDGE_TEST<-data.frame(
GROSS=c(10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000),
TAXWEDGE=c(30.1,30.4,30.7,30.9,29.1,28.9,28.6,28.5,27.9,27.2,27.1,27.0,27.0,26.8,25,24,23,21,19)
)
So my intention is to plot graph with plotly package and make some smooth line similar like function geom_smooth() from ggplot2.
library(data.table)
library(plotly)
dat <-as.data.table(SAMPLE_WAGES_TAX_WEDGE_TEST)
fig <- plot_ly(dat, x = ~GROSS, y = ~TAXWEDGE, name = "Before reform", type = 'scatter',
mode = 'lines',line = list(dash = "solid") )
I try with this line of code but I can't add smooth line so can anybody help how to solve this problem ?
There are two ways how to solve this issue:
1) Convert ggplot object into plotly
ggfig <- ggplot(dat, aes(x=GROSS, y=TAXWEDGE) ) +
geom_line() + geom_smooth()
ggplotly(ggfig)
2) geom_smooth() is based on the loess smoother. You need to fit the loess first and use it in add_ribbons in connection with plot_ly object. Check this great blog entry (the last example) for implementation guidance on loess and other smoothers.
Referring and complementing the answer in Adding a smoothed line to a plotly chart, you may directly use plotly with the shape = 'spline' option within the line marker specifications in order to get that type of smoothing (see e.g. https://en.wikipedia.org/wiki/Spline_interpolation):
plot_ly(SAMPLE_WAGES_TAX_WEDGE_TEST, x = ~GROSS, y = ~TAXWEDGE, type = 'scatter',
mode = 'lines', line = list(shape = 'spline', smoothing = 1.3))
where the optional parameter smoothing serves to (slightly) tune the degree of smoothing (see https://plotly.com/r/reference/scatter/#scatter-line-smoothing). Equivalently, using the specific add_lines trace function,
plot_ly(SAMPLE_WAGES_TAX_WEDGE_TEST, x = ~GROSS, y = ~TAXWEDGE) %>%
add_lines(line = list(shape = 'spline', smoothing = 1.3))
Related
I have a data set showing differences of two measurements of the same structure with two different methods as distance in meters and direction in degree. I found the scatterpolar plot of the function plot_ly of the package plotly in R, which produced almost what I wanted, but with some problems with the layout. Here is the code I used:
library(plotly)
data <- data.frame(measurements_compare); data
fig <- plot_ly(
type = 'scatterpolar',
r = c(data$distance),
theta = c(data$rotation),
text = c(data$id),
mode = 'markers',
)
fig
What I got from that was this plot, which is already quite close to what I want:
Now I would like to rotate the plot so that 0° is at the top instead of 90°, also I would like the degrees to be plotted ascending clockwise instead of counterclockwise. I found code examples for that in the archive where the function update_layoutwas used, but this example uses Python instead of R. I could not find something similar for R, but I am pretty sure there must be.
Inn plotly you can oftten use the same arguments in R and Python. For your code Rs layout function is needed instead of Pythons update_layout.
Example Data
data <- data.frame(distance = sample(seq(.1, .5, by = .01), 15, T),
rotation = sample(0:360, 15, T),
id = paste0(1:15))
Code
fig <- plot_ly(
type = 'scatterpolar',
r = c(data$distance),
theta = c(data$rotation),
text = c(data$id),
mode = 'markers',
) %>%
layout(polar = list(
angularaxis = list(
rotation = 90,
direction = "clockwise"
)
))
fig
Plot
I've searched all over for an answer to this issue. I have a plot created in visreg, and I have successfully changed the line and point colors with the following code.
visreg(RTmodel, "Verb_Type",
by = "Trust_Untrust", ylab = "LogRT", gg = T,
points = F, overlay = T) +
ggtitle("LogRT LME") +
scale_colour_manual(values = cb)
Awesome, just the colors I wanted! But the legend to the side is shaded with the original colors used by base Visreg. So, it doesn't really work with the colors I've chosen.
I've tried the following solutions:
Entering legend=FALSE to get rid of the legend.
Adding guides(color=guide_legend(title="Trustworthiness")) to the end, but that only creates a legend with the correct manually entered colors AND keeps an empty legend with the filled in boxes! (I've also tried this with legends=F and no dice).
.
visreg(RTmodel, "Verb_Type",
by = "Trust_Untrust", ylab = "LogRT", gg = T,
points = F, overlay = T, legend = F) +
ggtitle("LogRT ~ Verb Type by Trustworthiness") +
scale_colour_manual(values = cb) +
guides(color=guide_legend(title = "Trustworthiness"))
Entering plot=FALSE and creating a ggplot of the data visreg returns, but that comes with its own issues!
A previously given suggestion of adding one or a combination of the following:
.
fill=list(col=c("#56B4E9", "#D55E00")),
point=list(col=c("#56B4E9", "#D55E00")),
line=list(col=c("#56B4E9", "#D55E00"))
But I get the following error.
visreg(RTmodel, "Verb_Type",
by = "Trust_Untrust", ylab = "LogRT", gg = F,
points = T, overlay = T, legend = F,
fill = list(col = c("#56B4E9", "#D55E00")),
point = list(col = c("#56B4E9", "#D55E00")),
line = list(col = c("#56B4E9", "#D55E00")))
Error in plot.visreg(v, ...) :
formal argument "points.par" matched by multiple actual arguments
I've tried every combination I can in the visreg code T/F changes (gg, plots, legend etc). And the only two packages I have loaded are Tidyverse and Visreg. I think ultimately, this might be a bug in the system.
I am new in plotting time series. I downloaded a time series data and calculated a linear equation and I would like to add it in the time series plot. I want to show the year in the plot so I used index(stk) as x-axis input.
code:
library(quantmod)
stk <- suppressWarnings(getSymbols("AAPL", auto.assign = FALSE,
src = "yahoo", periodicity = "daily"))
stk <- na.omit(stk)
stk.lm1 <- lm(log(Cl(stk)) ~ c(1:nrow(stk)), data = stk)
plot(index(stk), log(Cl(stk)), type = "l", lwd = 3, las = 1)
abline(coefficients(stk.lm1)[1], coefficients(stk.lm1)[2], col="blue")
I know it is the plot using index(stk), how can I do to keep the x axis of plot in date and can I use plot.xts or other like ggplot2 to do the same things? Please advise, thank you very much.
It isn't dificult to do the plot that you want in base r plot or ggplot2 here is what you what:
plot(index(stk), log(Cl(stk)), type="l", lwd=3, las=1)
lines(x = index(stk.lm1$fitted.values), y = stk.lm1$fitted.values,col = "blue")
for the base r plot I added a line with the fitted values of the linear regression that I extracted with the $ signed and the dates of theme. Take into account that lm respect the structure of the data so the results are xts
library(ggplot2)
ggplot(stk, aes(x = index(stk), y = as.numeric(log(Cl(stk)))))+geom_line(lwd=1)+
geom_line(aes(x = index(stk.lm1$fitted.values), y = stk.lm1$fitted.values),col = "blue")+
labs(x = "Date", y = "Log Price")
For ggplot2 is quite similar. First you have to initiate the plot with ggplot where you defined the data and aesthetics (aes), then you add a line with geom_line and for the extra line I used the this command and define the new line in a new aes the same way I did it with the base r function.
Here's a ggplot solution. You shouldn't have to calculate the linear regression coefficients yourself:
# convert stk to data frame & specify your x-axis variable explicitly
stk.df <- as.data.frame(stk)
stk.df$Date <- as.Date(rownames(stk.df))
# plot
ggplot(stk.df,
aes(x = Date, y = log(AAPL.Close))) +
geom_line() +
geom_smooth(method = "lm", se = FALSE) +
labs(x = "Year", y = "log(AAPL's closing value)") +
theme_bw()
The geom_smooth line takes care of the regression. Set se = TRUE if you want to include a confidence interval around the line.
I'm trying to visualize a dataset using ggpairs from GGally library in R. I want to have a diagonal with density plots of each variable separated by a grouping variable. I'm not being able to get the right plots because of a scale issue. To illustrate my point, I'll use the following artificial dataset:
group=as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x=rnorm(100,group,1)
x[group==1]=(x[group==1])^2
y=2*x+rnorm(100,0,0.1)
data=data.frame(group=as.factor(group),x=x,y=y)
Using ggpairs, I get the following plot
library(ggplot2)
library(GGally)
ggpairs(data,columns = 2:3,colour="group")
Now, compare the top left plot to the density plot of variable x obtained using plain ggplot2:
ggplot(data, aes(x = x, colour = group)) + geom_density()
We can see that the y scale of the red and blue curves in ggpairs (the first figure) are not the same, which may lead to misleading conclusions. How can I correct this in ggpairs?
Here is the answer from the developers:
You are correct. they are not displaying correctly. :-(
With the current CRAN release, please try the following...
set.seed(1234)
group = as.numeric(cut(runif(100),c(0,1/2,1),c(1,2)))
x = rnorm(100,group,1)
x[group == 1] = (x[group == 1])^2
y = (2 * x) + rnorm(100,0,0.1)
data = data.frame(group = as.factor(group), x = x, y = y)
library(ggplot2)
library(GGally)
# # bad example
# ggpairs(data,columns = 2:3,colour="group")
ggally_correct_diag_densityDiag <- function(data, mapping, ...) {
# the color is corrected to fill by ggpairs
# to get desired output with color, it is changed back here.
if (! is.null(mapping$fill)) {
mapping$colour = mapping$fill
mapping$fill = NULL
}
ggplot(data, mapping) + geom_density(...)
}
ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = "correct_diag_density"))
Until the next release, you can leverage the eval process of ggpairs.
"ggally_FN_NAME" or "ggally_FN_NAMEDiag" are the naming conventions to
follow. The next release will allow for submission of custom functions
directly such as:
ggpairs(data, columns = 2:3, colour = "group", diag = list(continuous = ggally_correct_diag_densityDiag))
See more details in their github page
I am using following commands to produce a scatterplot with jitter:
ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T))
library(lattice)
stripplot(NUMS~GRP,data=ddf, jitter.data=T)
I want to add boxplots over these points (one for every group). I tried searching but I am not able to find code plotting all points (and not just outliers) and with jitter. How can I solve this. Thanks for your help.
Here's one way using base graphics.
boxplot(NUMS ~ GRP, data = ddf, lwd = 2, ylab = 'NUMS')
stripchart(NUMS ~ GRP, vertical = TRUE, data = ddf,
method = "jitter", add = TRUE, pch = 20, col = 'blue')
To do this in ggplot2, try:
ggplot(ddf, aes(x=GRP, y=NUMS)) +
geom_boxplot(outlier.shape=NA) + #avoid plotting outliers twice
geom_jitter(position=position_jitter(width=.1, height=0))
Obviously you can adjust the width and height arguments of position_jitter() to your liking (although I'd recommend height=0 since height jittering will make your plot inaccurate).
I've written an R function called spreadPoints() within a package basiclotteR. The package can be directly installed into your R library using the following code:
install.packages("devtools")
library("devtools")
install_github("JosephCrispell/basicPlotteR")
For the example provided, I used the following code to generate the example figure below.
ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5],500,replace=T))
boxplot(NUMS ~ GRP, data = ddf, lwd = 2, ylab = 'NUMS')
spreadPointsMultiple(data=ddf, responseColumn="NUMS", categoriesColumn="GRP",
col="blue", plotOutliers=TRUE)
It is a work in progress (the lack of formula as input is clunky!) but it provides a non-random method to spread points on the X axis that doubles as a violin like summary of the data. Take a look at the source code, if you're interested.
For a lattice solution:
library(lattice)
ddf = data.frame(NUMS = rnorm(500), GRP = sample(LETTERS[1:5], 500, replace = T))
bwplot(NUMS ~ GRP, ddf, panel = function(...) {
panel.bwplot(..., pch = "|")
panel.xyplot(..., jitter.x = TRUE)})
The default median dot symbol was changed to a line with pch = "|". Other properties of the box and whiskers can be adjusted with box.umbrella and box.rectangle through the trellis.par.set() function. The amount of jitter can be adjusted through a variable named factor where factor = 1.5 increases it by 50%.