Smoothing line data using SCIChart - scichart

Is it possible to automatically smooth data for a line chart, so that it displays as a nice graceful curve rather than jagged up-down lines? Or do I need to manually manipulate my data?
Edit: added an example of jagged-line chart
sciChartSurface = SCIChartSurface()
view.addSubview(sciChartSurface)
sciChartSurface.translatesAutoresizingMaskIntoConstraints = false
sciChartSurface.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
sciChartSurface.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
sciChartSurface.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
sciChartSurface.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
let xAxis = SCIDateTimeAxis()
xAxis.visibleRange = SCIDateRange(dateMin: Date().addingTimeInterval(-86400), max: Date().addingTimeInterval(2 * 86400))
xAxis.growBy = SCIDoubleRange(min: SCIGeneric(0.1), max: SCIGeneric(0.1))
xAxis.textFormatting = "MMM d"
sciChartSurface?.xAxes.add(xAxis)
let yAxis = SCINumericAxis()
yAxis.textFormatting = "%.1f"
yAxis.axisTitle = "Temperature"
sciChartSurface?.yAxes.add(yAxis)
lineDataSeries = SCIXyDataSeries(xType: .dateTime, yType: .double)
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(-86400)), y: SCIGeneric(28))
lineDataSeries.appendX(SCIGeneric(Date()), y: SCIGeneric(30))
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(86400)), y: SCIGeneric(26))
lineDataSeries.appendX(SCIGeneric(Date().addingTimeInterval(2 * 86400)), y: SCIGeneric(28))
let series = SCIFastLineRenderableSeries()
series.dataSeries = lineDataSeries
series.strokeStyle = SCISolidPenStyle(colorCode: 0xFF279B27, withThickness: 1.0)
sciChartSurface.renderableSeries.add(series)

SciChart supports a type of Spline interpolation via a custom series. You can see an example here:
iOS Custom Series Spline Line Example
This requires creating a custom series (full source code is provided in the example above, and also hosted on GitHub here.)
Edit: update
Scichart iOS/Android now supports spline line out of the box
https://www.scichart.com/example/ios-spline-line-chart/

Related

How to effectively adjust graph margin or padding in dash plotly

I have plotted two graphs using plotly dash. But when the y-axis / x-axis tick size is more it gets cut off.
Y-axis :
Code :
data = [go.Scatter(x = df[df['S2PName-Category']==category]['S2BillDate'],
y = df[df['S2PName-Category']==category]['totSale'],
mode = 'markers+lines',
name = category) for category in df['S2PName-Category'].unique()]
layout = go.Layout(title='Category Trend',
xaxis = dict(title = 'Time Frame', tickformat = '%d-%b-%y'),
yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log'),
hovermode = 'closest',
plot_bgcolor = colors['background'],
paper_bgcolor = colors['background'],
font = dict(color = colors['text'])
)
X-Axis :
Code :
data = [go.Scatter(x = df[df['S2PName']==item]['S2BillDate'],
y = df[df['S2PName']==item]['totSale'],
mode = 'markers+lines',
name = item) for item in items]
layout = go.Layout(title='Category Trend',
xaxis = dict(title = 'Time Frame' , tickformat = '%d-%b'),
yaxis = dict(tickprefix= '₹', tickformat=',.2f',type='log',autorange = True),
hovermode = 'closest',
plot_bgcolor = colors['background'],
paper_bgcolor = colors['background'],
font = dict(color = colors['text'])
)
In the above 2 graphs , as the length of the tick value increases, it gets cut off . Is there a better way to handle this ?
Credit for #Flavia Giammarino in comments for the reference to the docs. I'm posting the answer for completeness.
https://plotly.com/python/setting-graph-size/
From that link the example below shows how to set margin:
fig.update_layout(
margin=dict(l=20, r=20, t=20, b=20),
)
Where l r t b correspond to left, right, top, bottom.
I had a similar problem with some Dash/Plotly charts and long y axis labels being truncated or hidden. There didn't seem to be much information or documentation on this issue, so it took a while to solve.
Solution: add this code to the layout settings to prevent truncation of the y axes labels:
fig.update_layout(
yaxis=dict(
automargin=True
)
)
or you can update the yaxes setting specifically:
fig.update_yaxes(automargin=True)
Update: I tried another version of Plotly (5.10 or above) which mentions setting the automargin setting to any combination of automargin=['left+top+right+bottom'] with similar results. This still seems a bit unstable and doesn't solve all possible scenarios or corner cases, but works fine in most cases, especially when the browser window is maximized.

Converting LDAvis output to tiff

I'm working on topic modeling and I've recently discovered the excellent library 'LDAvis'. Unfortunately, the visual output of the library is in json, and I do not know how to convert it to tiff. This is the format required for graphs by most academic journals.
What I need is a way to convert the output of LDAvis to tiff.
Here an example I've derived from the 'text2vec' package, from here: https://github.com/dselivanov/text2vec/issues/235
library(text2vec)
library(magrittr)
data("movie_review")
tokens = movie_review$review[1:4000] %>%
tolower %>%
word_tokenizer
it = itoken(tokens, ids = movie_review$id[1:4000], progressbar = FALSE)
v = create_vocabulary(it) %>%
prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.2)
vectorizer = vocab_vectorizer(v)
dtm = create_dtm(it, vectorizer, type = "dgTMatrix")
lda_model = LDA$new(n_topics = 10, doc_topic_prior = 0.1, topic_word_prior = 0.01)
doc_topic_distr =
lda_model$fit_transform(x = dtm, n_iter = 1000,
convergence_tol = 0.001, n_check_convergence = 25,
progressbar = FALSE)
# this plots LDAvis in current session
lda_model$plot()
What I want to accomplish are graphs like this:
and this:
Unfortunately, this is the best I can do, by using of some kind of converter (press "print" the page, and it opens automatically, as a .pdf converter)
Thank you in advance.

Oxyplot showing HeatMapSeries behind ScatterSeries

I am trying to combine LineSeries, ScatterSeries and HeatmapSeries on a single OxyPlot instance. I am able to show the first two just fine on the same plot and it looks like the following:
The axes for this are generated by the following code:
var xAxis = new DateTimeAxis();
xAxis.Key = "X";
xAxis.Position = AxisPosition.Bottom;
xAxis.AbsoluteMinimum = DateTimeAxis.ToDouble(CurrentPass.AOS);
xAxis.AbsoluteMaximum = DateTimeAxis.ToDouble(CurrentPass.LOS);
xAxis.AxislineColor = xAxis.TextColor = xAxis.TicklineColor = xAxis.MajorGridlineColor = OxyColors.DarkGray;
var yAxis = new LinearAxis();
yAxis.Key = "Y";
yAxis.Position = AxisPosition.Left;
yAxis.AbsoluteMinimum = 0.0;
yAxis.AbsoluteMaximum = MaximumFrequency;
yAxis.Maximum = MaximumFrequency;
yAxis.AxislineColor = yAxis.TextColor = yAxis.TicklineColor = yAxis.MajorGridlineColor = OxyColors.DarkGray;
If I add in a third axes for the Heatmap (not even a HeatMapSeries yet), I get the following:
The extra code here for the axis for the HeatMap is:
var heatmapAxis = new LinearColorAxis();
heatmapAxis.AbsoluteMinimum = 0.0;
heatmapAxis.AbsoluteMaximum = MaximumFrequency;
heatmapAxis.Palette = OxyPalettes.Gray(1024);
heatmapAxis.Key = "HeatMap";
I am not sure what's going on here. The line series still shows. And all the ScatterPoints from the scatterseries I have are definitely there - the tracker shows up and I can interact with the points (hover, click etc.). But the points don't show. If I add a HeatMapSeries, the HeatMapSeries data does show up as expected, the LineSeries data still shows up but no ScatterSeries data.
Again, the HeatMap data and the Scatter Series data show up individually, but never together.
Has anyone encountered this before? Are there workarounds?
Thanks,
Aditya
Sorry this is a bit late but just stumbled upon this. I had something similar with a line not showing up on a Heatmap and after some experimenting i realized the order in which the series' are added to the plot model make a big difference. Make sure you add your ScatterSeries after you've added your Heatmap to the plot model.

plotting interaction from mixed model lme4 with CI bands

I have the following mixed effects model:
p1 <- lmer(log(price) ~ year*loca + (1|author), data = df)
'year' is continuous
'loca' is categorical variable with 2 levels
I am trying to plot the significant interaction from this model.
The following code (using the visreg package) plots the lines from each of the two 'loca' but it does not produce a 95% confidence band:
visreg(p1, "year", by = "loca", overlay = T,
line=list(lty = 1, col = c("grey", "black")), points=list(cex=1, pch=19,
col = c("grey", "black")), type="conditional", axes = T)
Then, I tried the following code which allows me to plot the lines, but with no data points on top and no CIs:
visreg(p1, "year", by = "loca", overlay = T,
line=list(lty = 1, col = c("grey60", "black")), points=list(cex=1,
pch=19, col = c("grey", "black")),
type="conditional", trans = exp, fill.par = list(col = c("grey80",
"grey70")))
I get CI bands when I use type = 'contrast' rather than 'conditional'. However, this doesn't work when I try to backtransform the price as above using trans = exp.
Overall I need to be able to plot the interaction with the following attributes:
Confidence bands
backtransformed points
predicted line (one for each level of 'loca')
More than happy to try other methods....but I can't seem to find any that work so far.
Help much appreciated!
one possibility is with the use of the effects package:
library(effects)
eff.p1 <- effect("year*loca", p1, KR=T)
then you could either directly plot it with what the package provides and customize it from there:
plot(eff.p1)
or take what effect produces and plot it with ggplot in a nicer plot:
eff.p1 <- as.data.frame(eff.p1)
ggplot(eff.p1, aes(year, linetype=factor(loca),
color = factor(loca))) +
geom_line(aes(y = fit, group=factor(loca)), size=1.2) +
geom_line(aes(y = lower,
group=factor(loca)), linetype =3) +
geom_line(aes(y = upper,
group=factor(loca)), linetype =3) +
xlab("year") +
ylab("Marginal Effects on Log Price") +
scale_colour_discrete("") +
scale_linetype_discrete("") +
labs(color='loca') + theme_minimal()
I can't really try the code without the data, but I think it should work.
This should do the trick:
install.packages(sjPlot)
library(sjPlot)
plot_model(p1, type = "int", terms = c(year,loca), ci.lvl = 0.95)
Although it comes out with some warnings about labels, testing on my data, it does the back transformation automatically and seems to work fine. Customising should be easy, because I believe sjPlot uses ggplot.
EDIT: #Daniel points out that alternative options which allow more customization would be plot_model(type = "pred", ...) or plot_model(type = "eff", ...)

Writing functions in R - calling external functions from libraries

So I am trying to take a bit of code that I use for interactive selection and identification. It works outside of a function but gives an error when I try to run it as a stand alone function.
my.identify <- function(data)
{
# allows you to create a polygon by clicking on map
region = locator(type = "o")
n = length(region$x)
p = Polygon(cbind(region$x, region$y)[c(1:n,1),])
ps = Polygons(list(p), ID="region")
sps = SpatialPolygons(list(ps))
# returns all data that overlaps new polygon sps
a=data[!is.na(overlay(data,sps)),] # here is the problem
return(a)
}
Basically it doesn't want to run the overlay function (function of the sp package). The error report is that I can't run the inherited functions??
Error in function (classes, fdef, mtable) : unable to find an
inherited method for function "overlay", for signature "matrix",
"SpatialPolygons"
Any ideas??? I'm new to function writing... so hopefully it will be easy.
This should work. overlay is deprecated and over should be used instead. The catch is that all objects should be Spatial*.
xy <- data.frame(x = runif(40, min = -200, max = 200),
y = runif(40, min = -200, max = 200))
plot(xy)
my.identify <- function(data) {
# allows you to create a polygon by clicking on map
region = locator(type = "o")
n = length(region$x)
p = Polygon(cbind(region$x, region$y)[c(1:n,1),])
ps = Polygons(list(p), ID="region")
sps = SpatialPolygons(list(ps))
# returns all data that overlaps new polygon sps
a=data[!is.na(over(SpatialPoints(data),sps)),]
return(a)
}
ident <- my.identify(xy)
points(ident, pch = 16)
You need to add a call to the package in your function:
my.identify <- function(data)
{
require('sp') ## Call to load the sp package for use in stand alone function
# allows you to create a polygon by clicking on map
region = locator(type = "o")
n = length(region$x)
p = Polygon(cbind(region$x, region$y)[c(1:n,1),])
ps = Polygons(list(p), ID="region")
sps = SpatialPolygons(list(ps))
# returns all data that overlaps new polygon sps
a=data[!is.na(overlay(data,sps)),]
return(a)
}