Add a vertical line to a line chart from the googleVis - r

What I would like to do is add a vertical line to a line chart in R from the googleVis package. Does anyone know if this is supported? For example, given the following simple line chart, I would like to add a vertical line at x=2. Is this even possible?
# Simple example. Must bring in 'googleVis' package first.
df <- data.frame(country=c(1,2,3), val1=c(1,3,4), val2=c(23,12,32))
Line1 <- gvisLineChart(df, xvar="country", yvar=c("val1", "val2"))
plot(Line1)

I realized how to do it by asking the developer. You use NAs to get a vertical reference line like this:
library(googleVis)
dat <- data.frame(x=c(2,2,1,3,4),
y1=c(0,3,NA,NA,NA),
y2=c(NA,NA,0,3,2))
plot(gvisScatterChart(dat,
options=list(lineWidth=2,
pointSize=2))
)

Related

Align plotly plots in R-Markdown

Can someone tell me if there is a way to align plotly plots in R-Markdown?. More specifically: Currently my plots are being placed one after another. I would like to have a grid-like format. I achieved this before, using the simple plot function, but that doesn't seem to work with plotly.
The following is an example of my prior code, that worked with the simple plot function. Can I make that work, assuming I were using plotly?
Thanks in advance.
{r comment = NA,fig.width=14, fig.height=14}
layout(matrix(c(1,2,3,4,5,6,7,8), 4, 2, byrow = TRUE))
m_alt <- miete[miete$baujahr <= 22,]
m_neu <- miete[miete$baujahr > 22,]
plot(table(m_alt$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_neu$bezirk),main="",ylab="Frequency",xlab="Bezirk")
plot(table(m_alt$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_neu$wohnflaeche),main="",ylab="Frequency",xlab="Wohnfläche")
plot(table(m_alt$wohnlage,m_alt$zimmerzahl),main="Altbau: Wohnlage u. Zimmerzahl")
plot(table(m_neu$wohnlage,m_neu$zimmerzahl),main="Neubau: Wohnlage u. Zimmerzahl")
plot(table(miete$bezirk,miete$zimmerzahl),main="Bezirk u. Zimmerzahl")
plot(table(miete$warmwasser,miete$bezirk),main="Warmwasser u. Bezirk")

RGraphviz Hasse Diagram displaying only one lable

In order to create an Hasse Diagram like the following
One is using the following libraries
library(rPref)
library(Rgraphviz)
One is taking a small sample of one's data
df <- data[1:10,]
Then creating the preferences
pref <- low(time) * low(MAPE)
And the Better-Than-Graph (BTG)
btg <- get_btg(df, pref)
In order to display the labels for the nodes containing relevant values, one is creating the labels as following
labels <- paste0(df$time, "\n", df$MAPE)
However, when one builds the visualization with
plot_btg(df, pref, labels)
One can only see the first label, instead of the two. Here is what one is seeing
Passing use_dot=FALSE solved the problem
plot_btg(df, pref, labels, use_dot = FALSE)

R and Gviz: How to remove (/crop) a region of a plot?

This code:
alTrack <- Gviz::AlignmentsTrack(
system.file(package = "Gviz", "extdata", "gapped.bam"),
isPaired = TRUE)
Gviz::plotTracks(
alTrack,
from = 3048500,
to = 3049000,
chromosome = "chr12")
Produces this plot:
Using the Gviz package. My problem is the empty space that's always appearing in the bottom of the picture. How can I remove a specific region of this plot / crop to only keep the region I need? If I could remove the lower 15% of a produced plot with an R function that would be great.
The specific region I want to not appear:
The max.height parameter will do it.
Already answered here:
https://support.bioconductor.org/p/81044/

Suppress default text in tooltip using plot_ly

With plot_ly(), I am able to add what I want in the tool tip, but I don't manage to get rid of the default value. Is there way to do this ?
In the example below, for the first point, the tooltip is "250 company1". I would like to get only "company1". I have a solution using ggplot2 and then ggplotly() with the tooltip option, but I would rather remain with plotly only.
require(plotly)
seq <- 1:10
name <- c(paste0("company",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,value)
plot_ly(data=d,x=seq,y=value,text=name)
You need the hoverinfo parameter for plot_ly:
seq <- 1:10
name <- c(paste0("company",1:10))
value <- c(250,125,50,40,40,30,20,20,10,10)
d <- data.frame(seq,name,value)
plot_ly(data=d,x=seq,y=value,text=name,hoverinfo="text")
More reading: plotly R chart attribute reference
Gopala, I asked a similar question and found the answer useful. Perhaps it will help you too.
How can I remove the size line in the hoverinfo of a Plotly chart in R?

ggplot2 and TikzDevice - Removing White Space

I am using tikzDevice package to obtain Latex-friendly graphs in R. I am having trouble in removing excessive white spaces from top and bottom of the graph below:
I have tried using par(mar) but it does not seem to work with ggplot2. Also, theme(plot.margins) seems to unresponsive as well. The white space is introduced as I try to alter the aspect ratio of the figure with theme(aspect.ratio).
Any suggestions?
Thanks!
EDIT: Here is a MWE:
library(tikzDevice)
library(reshape2)
x = seq(0,1,0.1)
y1 = x^2+2*x+7
y2= x^+2*x+2
df = data.frame(x,y1,y2)
df <- melt(df, id.vars=c("x"))
names(df) <- c("x","$latex~Name$","value")
plot <- ggplot(df,aes(x=x,y=value,color=`$latex~Name$`,group=`$latex~Name$`)) + geom_line() +
theme(aspect.ratio = 0.4)
plot
tikzDevice(file="mweTex.tex")
plot
dev.off()
The problem seems to be caused by some tikz statements that affect the bounding box of the image. Since there doesn't seem to be any option in tikzDevice for minimizing whitespace, I had to think of something else. I managed to fix the generated tikz file by adding the following R code at the end of your MWE:
# remove all lines that invisibly mess up the bounding box
lines <- readLines(con="mweTex.tex")
lines <- lines[-which(grepl("\\path\\[clip\\]*", lines,perl=F))]
lines <- lines[-which(grepl("\\path\\[use as bounding box*", lines,perl=F))]
writeLines(lines,con="mweTex.tex")
I've tested it on your MWE:
The left image is without the fix, the right image is with the fix.

Resources