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.
Related
I am trying to put grids in my barplots, but they appear in front of the data and not in the background. I tried to fix this using
panel.first = grid()
As for the data I am trying to plot, the first column consists of the year numbers (2014-2021) and the second columns are the corresponding values (all vector classes are numeric). When trying to plot using the following code:
par(mfrow=c(1,2))
barplot(mean_trend[,2],names.arg = mean_trend[,1],col="skyblue",ylim = c(0.1,95),cex=0.8,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
mtext(side=2,line=2.3, "Average amount in mm", font=2, cex=0.8)
box(lwd=1.5)
barplot(freq_trend[,2],names.arg = freq_trend[,1],col="skyblue",ylim = c(2,4500),cex=0.85,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
box(lwd=1.5)
mtext(side=2,line=3.3, "Average flood frequency", font=2, cex=0.8)
I obtain the following result
As you can see, the grid is now behind the plotted data, but exceeds the box/plot limits. How can I fix this?
Kind regards
As you didn't add a data mean_trend - I give an example with other data.
About add and others arguments - you can read ?barplot
# One row, two columns
par(mfrow = c(1, 2))
#PLOT1
barplot(table(mtcars$cyl), main = "PLOT 1", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)
#PLOT2
barplot(table(mtcars$cyl), main = "PLOT 2", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)
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")
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)
I am creating 2 overlapping histograms on a single plot. The diagram needs to be clear in black and white, so I am using a combination of colours and textures to help distinguish between the histograms.
The problem is with the legend, where I can either get the colours to show up, or the textures, but not both. This code produces a legend with empty boxes:
dat <- rnorm(100)
dat2<-rnorm(100,1)
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA), density = c(0, 20))
I don't have the reputation to post images, but a link to the output is below:
https://drive.google.com/uc?id=0B4VH8cX4Cf7bNjhVQnUwWWVaMEk
I can get the colour alone to display correctly:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", NA))
https://drive.google.com/uc?id=0B4VH8cX4Cf7bcXJvakZxN0x5c1U
And the textures alone also works:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), density = c(0, 20))
(I've can only post 2 links, also because I don't have enough reputation)
It just won't work together. What am I doing wrong?
Thanks!
Figured it out. The fill argument in legend() also applies to the shading lines, so when I specified that fill for the second legend item was NA, it meant the shading lines were invisible.
The following code produces the result I want:
hist(dat, col = "grey", xlim = c(-3, 5), ylim = c(0,35))
hist(dat2, density = 20, add = TRUE)
legend("topright", legend = c(1,2), fill = c("grey", "black"), density = c(1000, 20))
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?