line break and subscript in axis title using plotly in R - r

I just started to use plotly for some interactive scatter plots in R and having a hard time on axis labels. Normally I designed my plots with ggplot2 and then using the ggplotly function to convert them, but this is sometimes very slow for any reason. So I want to create my plots directly in plotly...
I am trying now to change the axis title and want to add line breaks and later I also want to add subscript labels. But I am already failing at the newline in the title. Is there any trick?
library(plotly)
library(dplyr)
plot_ly(mtcars, x = wt, y = mpg, text = rownames(mtcars), mode = "text") %>%
layout(xaxis=list(title='text with\nlinebreak'))

In plotly, you can get linebreaks (and other text formatting) using html tags.
So piping
layout(xaxis=list(title='text with <br> linebreak'))
should work.
Hence, to get subscript labels use the <sub> tag. For example
CO<sub>2</sub>
will give you
CO2.

Note you have to use <br> or <br /> not <br/> (without a space)

Related

Printing out huge plot so I can see the results

I am using GGally::pairs to look at 63 columns of numerical data. The function runs fine but the result is a plot where I can't read any of the column names fully or the numbers (correlations etc.). The resolution is fine, it is just that the plot is compressed so that the plots are tiny and cant really be read (and only part of the text is visible). This is because it prints the plot to the plot viewer in Rstudio I suppose. How can I get the plot to be bigger so I can actually see the results
Revising my comment above, instead of ggsave() that I'm not sure can work, this might work, but assumes plotly accommodates the geoms in your plot (it does for a good number of ggplot geoms, but not all, eg many of the library(ggridges)'s geoms).
library(plotly)
library(htmlwidgets)
p <- ggplot(your ggplot data/parameters/geoms) #creates ggplot object
p <- ggplotly(p) #converts p to html, renders in RStudio console
htmlwidgets::saveWidget(as_widget(p), "filename.html") #saves as a single html file

Textbox in gridarrange

I am arranging two graphs on a pdf using grid.arrange.
I would like to have a text box between the title and the first plot to add some information on the graphs themselves possibly editing in in rich text.
What is the best solution for that?
I would ideally produce it in R directly and printing it out with the rest of the plot.
Any idea?
To use a standard example of grid.arrange
pl = replicate(3, ggplot(), FALSE)
grid.arrange(grobs = pl)

text() in R on plot - how to keep on a single line without wrapping?

I often use R to run batch jobs that contain PDF outputs with scatter plots produced using a combination of plot() and points(), among other graphical functions. (I don't use ggplot2 much and would like to avoid using it for this question.)
When using the text() function in a plot in order to add text near a plotted symbol, I like to use the pos = 4 option to right-justify text next to say a symbol like pch = 23 (filled diamond). But I've noticed that sometimes the text will get wrapped as part of multiple lines, and other times it stays on a single line. Unfortunately when the lines are wrapped it causes text overlap problems. Why does text() sometimes wrap text on multiple lines?
Is there a way to force R to keep the text added to a plot from text() on a single line?
Here is my code:
text(x=data_frame_w_data_to_plot$x_axis_value,
y=data_frame_w_data_to_plot$y_axis_value, labels=data_frame_w_data_to_plot$text_to_plot, col="black", cex=1, pos=4)
Answering my own question here. Found the text string of "\n" just happened to be in one of the cells, and simply used gsub() to slightly modify this cell. This solved the problem. Learn from my mistake. :)
data_frame_w_data_to_plot$text_to_plot <- gsub("\n", " ", data_frame_w_data_to_plot$text_to_plot)

R plotting using a table command - text labels missing

I have code below. Why don't the text labels appear? I want them on top of the each bar.
x=c(20,30,70,5,10,20)
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs")
text(aaa,labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
Your text statement only gives the argument aaa to position the text labels. So text is plotting your points at (1,5), (2,10), (3,20), (4,30) and (5,70) - all of which are off the graph and so do not display. You can get almost what you want by changing your text statement to:
text(aaa, table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)
But that cuts off the highest label a little, so I recommend adjusting ylim on the plot.
aaa=plot(table(x),yaxt='n',main="county", ylab="drop offs",
ylim=c(0,max(table(x)))+0.1)
text(aaa,table(x), labels=c(1,2,3,4,5),pos=3,col="red",cex=0.8)

R ggplot2 arrowheads with geom_text

I use geom_text to plot some text. A text example would be:
a -> 7.0
However, instead of using "->" as an arrowhead I would like to use an arrowhead symbole.
I found the package plotmath to be used with ggplot to plot symboles.
I am not sure if it wouldn't be easier to plot the text separated from the actual symbol and align them using different coordinates that are close together so that once plotted it looks like a single string.
You'll want to pass it as an expression and use the parse=TRUE option for geom_text. The geom_text documentation should provide you with enough to get going:
Plotmath with geom_text: link.

Resources