r- Unclear abbreviations of cases' labels on plot - r

I have a problem with display of abbreviated cases' names on the following plot.
The code I use:
install.packages("HSAUR2")
library(HSAUR2)
data("Forbes2000", package="HSAUR2")
head(Forbes2000)
attach(Forbes2000)
not_na<-subset(Forbes2000,complete.cases(Forbes2000)
order_profits<-(order(not_na$profits))
top50<-not_na[order_profits[1946:1995], c("name","sales","profits", "assets")]
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
top50$assets,top50$sales, abbreviate(name,1, strict="TRUE",named="FALSE"), cex= .7,pos=4)
I tried to make it differently:
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
ab<-abbreviate(name,1, strict="TRUE",named="FALSE")
text(top50$assets,top50$sales, labels=ab, cex= .7,pos=4)
But had the same result.
If I use full labels instead, the display is ok, but plot gets messy:
plot(top50$sales~top50$assets,col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 1)
text(top50$assets,top50$sales, labels=top50$name, cex= .7,pos=4)
Could you show, how to make abbreviations look well? Is there any mistake in my code or the problem is due to soft issues?

Related

how to match points in dot plot with factors in R?

I want a dot plot for mydf below
mydf <- data.frame(city=c(rep(c("Rome","NY","LA"),3)),
old=c(11,23,13,24,12,13.5,15,17,22),
new=c(12,22,13.5,25,14,15,12,17,14),
method=c("a","a","a","b","b","b","c","c","c"))
my_cols <- c("red", "blue", " dark green")
grps <- as.factor(mydf$method)
dotchart(mydf$old, labels = mydf$city,
groups = grps, gcolor = my_cols,
color = my_cols[grps],
cex = 0.6, pch = 19, xlab = "value")
meth1<- mydf[mydf$method=="a",]
meth2<- mydf[mydf$method=="b",]
meth3<- mydf[mydf$method=="c",]
points(meth1$new, 1:nrow(meth1), col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:nrow(meth2), col = "green", pch = 16, cex = 0.6)
before adding points(), I get plot below, which is what I want as a basis.
but when I add points, all of them appear in the bottom part of the plot. I want the "new" values corresponding to each method appear in its own part of the plot and a segment line connects the old and the new values accordingly.
how can I do that in my code? thanks for any help with this.
The y-axis in the dotchart, under the hood, is really just integers. So you have to provide the correct y-axis values to points():
points(meth1$new, 11:13, col = "orange", pch = 16, cex = 0.6)
points(meth2$new, 6:8, col = "light blue", pch = 16, cex = 0.6)
points(meth2$new, 1:3, col = "green", pch = 16, cex = 0.6)
I reached those values by just eyeballing the plot and counting up. Note that not all these points will appear if they are outside the range of the original dotchart call. You can adjust that by setting xlim = c(11,25) or something appropriate in the dotchart call.

In base R plot, how can deploy multiple pchs to the same symbol?

plot(1, pch = 19, cex = 3, col = "red")
points(1, pch = 1, cex = 3, col = "black", lwd = 2)
legend("top",
"sym",
pch = 19,
col = "red",
cex = 2,
pt.cex = 4)
legend("top",
"sym",
pch = 1,
col = "black",
cex = 2,
pt.cex = 4)
In R, a black borderline can be added to a solid red circle like:
Then, how can I use the symbol with added types for the legend?
As the code shows, legends cannot be added but overlapped.
Thanks.
Using bty = "n"
plot(1, pch = 19, cex = 3, col = "red")
points(1, pch = 1, cex = 3, col = "black", lwd = 2)
legend("top",
"sym",
bty = "n",
pch = 19,
col = c("red"),
cex = 2,
pt.cex = 4)
legend("top",
"sym",
bty = "n",
pch = 1,
col = c("black"),
cex = 2,
pt.cex = 4)
Created on 2019-02-14 by the reprex package (v0.2.1)
Anoter option is to use pch symbol that works with background option, it can be used only when pch = 21:25.
plot(1, pch = 19, cex = 3, col = "red")
points(1, pch = 1, cex = 3, col = "black", lwd = 2)
legend("top",
"sym",
pch = 21,
pt.bg="red",
cex = 2,
pt.cex = 4)

R specifying point border color differently from fill and line

This is related to points border color and line color is different between legend box and whole plot box when pch=21
I have a plot with filled point (pch=21), black border and green filled, but I also have whiskers for the error that are the same color as the fill color. I know I can match the point in the legend using a combination of "col" and "pt.bg", but then the line color matches the point border, which is not what I need:
x=1:10
y=runif(10)
plot(x,y,lwd=3,col="black",bg="green",pch=21,cex=2)
arrows(x,y-0.05,x,y+0.05,lwd=3,col="green",angle=90,code=3)
legend(5,0.8,col="black",pt.bg="green",lwd=3,pch=21,legend="text",cex=2)
gives the following...
Is there a way of having legend green lines and green filled points with the point border black? Even nicer would be having the whiskers, but I think that is probably not possible...
Make two legend calls. One to plot the line with bg = NA and the other to plot the point with bty = "n":
set.seed(1)
x=1:10
y=runif(10)
plot(x, y, lwd = 3, col = "black", bg = "green", pch = 21, cex = 2)
arrows(x, y-0.05, x, y+0.05, lwd = 3, col = "green", angle = 90, code = 3)
legend("topright",
col="green",
lwd = 3,
lty = 1,
legend = "text",
cex = 2,
bg = NA)
legend("topright",
col = "black",
pt.bg = "green",
pch = 21,
lwd = 3,
legend = "text",
cex = 2,
lty = 0,
bty = "n")

Is it possible to change the shape of the "images" in the legend of an igraph plot?

Using:
legend(x=0, y=-1.2, xjust = 0.5, ncol=2,
c("men", "women"), pch=21, col="black",
pt.bg=c("gray", "gray"), pt.cex=1.5)
I get this legend on an igraph plot:
However I need the shape of "men" to be a square. Is that possible? And if so then how?
If you set the parameter pch = 22 the legend will display squares instead of circles. For different symbols use pch = c(21, 22). For more information on controlling the plotting character, type ?pch in the console.
Control the legend shapes as you would colors, text....
legend(x = 10, y = 100, xjust = 0.5, ncol = 2, c("men", "women"),
pch = c(22, 21),
col = "black", pt.bg = c("gray", "gray"), pt.cex = 1.5)

Adjust the font of legend in R

I use legend() to produce a legend shown below
The text lies beyond the plot box. I tried to use cex = to adjust the box, however, it can only adjust the size of the whole box, but have nothing to do with the text font.
Is there anyway to make the text font smaller?
Here is my sample code:
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90')
You can set graphical parameters by applying par(). For example:
plot(c(1:4), c(1:4), type = 'l')
par(cex = 1) #set legend font to 1
legend("topleft", legend="a line", lty = 1)
If you set bty="n" it won't draw a box
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90',bty="n")
Try to hold the pt.cex parameter to 1, while trying different values for cex inside the legend call. pt.cex control the size of points and lines of the legend.
x <- rnorm(100, 10, 4)
y <- rnorm(100, 10, 4)
plot(x, y, type = "n")
## I tried to feed cex with 1.1 and 0.4. The font size changes while the lines remain unchanged.
legend("bottomleft", legend = c("Simulated", "Estimated/Predicted
Median", "95% Credit Intervals"),
col = c("gray35", "red", "red"), lty = c(1, 1, 2),
lwd = c(3, 2, 1),
text.font = 3, inset=.02, bg='gray90', pt.cex = 1, cex = 0.4)
As you can see, the size size of the font change while the lines remain almost the same. Try play with them until you do not find the correct proportions for your plot.

Resources