plot() won't change axis text size using cex.axis - r

I am trying to plot a likert scale of a 1 - 7 scale for every country with the likert package in R, but the R base plot() function won't change any of the text sizes (or even add a main title).
I am generating the plot with:
p <- likert(summary = data )
plot(p,
plot.percents=FALSE,
plot.percent.low=FALSE,
plot.percent.high=FALSE,
include.center=TRUE,
plot.percent.neutral = FALSE,
col = c(brewer.pal(n = 7, name = "RdBu")),
cex.axis = 0.5,
cex.lab=0.5,
main = "title")
Which produces the following plot:
Plot of Countries of the World
All the other plot parameters are working, so I'm not sure why the last 3, the most basic aren't working.

I fixed this by using theme(axis.text) like so:
plot(p,
plot.percents=FALSE,
plot.percent.low=FALSE,
plot.percent.high=FALSE,
include.center=TRUE,
plot.percent.neutral = FALSE,
col = c(brewer.pal(n = 7, name = "RdBu")),
main = "title") +
theme(axis.text=element_text(size=4)
This doesn't answer the larger question as to why the plot() function isn't accepting the parameters, as the title still isn't showing, but it's a band aid solution

Related

How to export an annotated plot made with ggarrange using cairo so that the plot boundary lines are removed?

I want to make a graphic made with ggarrange, use shared labels for the axis and export it with cairo to have a nice image.
The problem is that when I add it to latex the graphic is surrounded by a line which crosses the axis labels. How can I remove that line from the graphic?
6 plots (m1_g to m6_g) are combined with ggarrange to make a graphic called figure, then its annotated with annotate_figure to produce the graphic called figure2.
figure = ggarrange(m2_g+rremove("ylab")+rremove("xlab"),m1_g + rremove("ylab")+rremove("xlab"),m3_g +rremove("xlab")+ rremove("ylab"),m4_g +rremove("xlab") + rremove("ylab"),
m5_g +rremove("xlab")+ rremove("ylab"),m6_g +rremove("xlab")+ rremove("ylab"),
common.legend = TRUE, legend="right",ncol = 6)
figure2 = annotate_figure(figure, left = textGrob("Voltaje [mV]", rot = 90, vjust = 1, gp = gpar(cex = 1.3)),bottom = textGrob("Tiempo E-1 [s]", gp = gpar(cex = 1.3)))
ggsave("measurements_P4.png",plot = figure2, width = 10,type = "cairo")
`
This is the problem i am facing

Plotting titles and legends on interactive igraph plot? (R)

I'm having trouble adding a title and legend to the inside of my graph. Is it not possible with tkplot OR plot?
dff <- data.frame(a = c(0,1,2,3,4),b = c(3,4,5,6,7))
nod <- data.frame(node = c(0:7),wt = c(1:8))
pg <- graph_from_data_frame(d = dff, vertices = nod,directed = F)
# plot function with edge.label added
tkplot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')
It doesn't seem to want to work the way I want it to. I just want the legend to show the vertices adjusted to be bigger for a higher degree.
I was also wondering if there is a way to plot only certain vertices from the graph? For example, you choose one vertex and plot only the vertices that form a path back to it?
Based on the help for tkplot, here's an example of how to add a title to an interactive plot:
library(tcltk)
id = tkplot(pg, edge.label = nod$wt, main = "this is my graph")
canvas = tk_canvas(id)
width = as.numeric(tkcget(canvas, "-width"))
height = as.numeric(tkcget(canvas, "-height"))
tkcreate(canvas, "text", width/2, 25, text="My title",
justify="center",
font=tkfont.create(family="helvetica", size=20, weight="bold"))
For the standard base R plot, your code works as is:
plot(pg, edge.label = nod$wt, main = "this is my graph")
legend('topleft',legend= degree(pg)*5,pt.cex=20,col='white',
pch=21, pt.bg='white')

Plotting in R using plot function

I am trying to plot few graphs using loops. I am now describing in details.
First I have a function which is calculates the y-variable (called effect for vertical axis)
effect<- function (x, y){
exp(-0.35*log(x)
+0.17*log(y)
-0.36*sqrt(log(x)*log(y)/100))
}
Now I run the following code and use the option par to plot the lines in the same graph. I use axis=FALSE and xlab="" to get a plot without labels. I do this so that my labels are not re-written each time the loop runs and looks ugly.
for (levels in seq(exp(8), exp(10), length.out = 5)){
x = seq(exp(1),exp(10), length.out = 20)
prc= effect(levels,x)
plot(x, prc,xlim = c(0,max(x)*1.05), ylim=c(0.0,0.3),
type="o", xlab = "",ylab = "", pch = 16,
col = "dark blue", lwd = 2, cex = 1, axes = F)
label = as.integer(levels) #x variable
text(max(x)*1.03,max(prc), label )
par(new=TRUE)
}
Finally, I duplicate the plot command this time using the xlab and ylab options
plot(x, prc, xlab = "X-label", ylab = "effect",
xlim = c(0,max(x)*1.05), ylim = c(0,0.3),
type="l", col ='blue')
I have several other plots in the similar lines, using complex equations. I have two questions:
Is there an better option to have the same plot with smoother lines?
Is there an easier option with few lines to achieve the same, where I can place the texts (levels) for each line on the right with white background at the back?
I believe working with the plot function was tedious and time consuming. So, I have finally used ggplot2 to plot. There were several help available online, which I have used.

How to change the legend title and position in a lattice plot

I'm using lsmip from lsmeans to plot my model,
library(lsmeans)
PhWs1 <- lsmip(GausNugget1, Photoperiod:Ws ~ Month,
ylab = "Observed log(number of leaves)", xlab = "Month",
main = "Interaction between Photoperiod and Water stress over the months (3 photoperiods)",
par.settings = list(fontsize = list(text = 15, points = 10)))
but I was not able to get a suggestion on the internet on how to handle the legend position, size, title, etc.
I used trellis.par.get() to see the parameters but I could not find the one related to my issue. As you can see from the graph, the legend should be "Photoperiod*Ws" but Ws is not visible.
I see two possibly complementing alternatives to approach this issue. The first would be to create a fully customized legend and pass it on to the key argument of xyplot (which lsmip is heavily based on). Here is an example taken from ?lsmip to clarify my point.
## default trellis point theme
trellis_points <- trellis.par.get("superpose.symbol")
## create customized key
key <- list(title = "Some legend title", # legend title
cex.title = 1.2,
x = .7, y = .9, # legend position
points = list(col = trellis_points$col[1:2], # points
pch = trellis_points$pch[1:2],
cex = 1.5),
text = list(c("A", "B"), cex = .9)) # text
## create results and extract lattice plot
d <- lsmip(warp.lm, wool ~ tension, plotit = FALSE,
main = "Some figure title", key = key)
p <- attr(d, "lattice")
p
As you can see, setting up a customized legend let's you modify all the different components of the legend - including labels, text and symbol sizes, legend spacing, etc. Have a deeper look at the key argument described in ?xyplot which describes the various modification options in detail.
Now, if you have a long legend title and you do not want to include the legend inside the plot area, you could also define separate viewports, thus allowing the legend to occupy more space at the right margin. Note the use of update to remove the initially created legend from p and the subsequent assembly of the single figure components using grid functionality.
## remove legend from figure
p <- update(p, legend = NULL)
## assemble figure incl. legend
library(grid)
png("plot.png", width = 14, height = 10, units = "cm", res = 300)
grid.newpage()
## add figure without legend
vp0 <- viewport(x = 0, y = 0, width = .75, height = 1,
just = c("left", "bottom"))
pushViewport(vp0)
print(p, newpage = FALSE)
## add legend
upViewport(0)
vp1 <- viewport(x = .7, y = 0, width = .3, height = 1,
just = c("left", "bottom"))
pushViewport(vp1)
draw.key(key, draw = TRUE)
dev.off()

How to plot a histogram using grid graphics in R?

I want to make a plot that is similar to beanplot or violin plot, but instead of the symmetric shapes, i want to plot two different distributions on the different sides of the center line. Something like figure 4 in this article [pdf]http://www.jstatsoft.org/v28/c01/paper.
I want to use the grid graphics engine in R. Any pointers will be useful. I looked at the lattice package and the histogram function in lattice package, but that is not what i want to do.
Any help/pointers are appreciated.
You can reasonably easily get the half-violin plot with fairly simple modifications to the function, panel.violin in Sarkar's lattice package. There are four lines in that function that can be altered inside the grid.polygon call to change from "two-sided" density plotting to single sided density plotting. First require(lattice) and require(grid). Then type panel.violin. I'll show you the "horizontal" alteration:
require(grid)
panel.violin2 <-
# snipped all the arguments and processing
grid.polygon(x = c(dx.list[[i]] ),
# Notice I removed: ... ,rev(dx.list[[i]])
y = c(2*dy.list[[i]] ), default.units = "native",
# Removed: ... , -rev(dy.list[[i]])
name = trellis.grobname(identifier, type = "panel",
group = group), gp = gpar(fill = col, col = border,
lty = lty, lwd = lwd, alpha = alpha))
Also remove it from the corresponding section in the else{ ...} clause. Now you can run it with the example in help(panel.violin)
bwplot(voice.part ~ height, singer,
panel = function(..., box.ratio) {
panel.violin2(..., col = "transparent",
varwidth = FALSE, box.ratio = box.ratio)
panel.bwplot(..., fill = NULL, box.ratio = .1)
} )
If you wanted the density on the other side, all you would need to do would be remove the dx.list[[i]] and dy.list[[i]] and leave in the rev(dx.list[[i]]) and -rev(dy.list[[i]]).

Resources