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))
Related
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.
I make a heatmap with the package ComplexHeatmap, but when i zoom in the figure, the colorbar shows a orrible white lines, like this:
Colorbar with orrible white lines
I tried jpeg, tiff and pdf.....same result when i zoom in the figure.
Here the code to draw sample my colorbar:
col_princ <- circlize::colorRamp2(breaks = c(1, 5, 9),
colors = c("red", "yellow", "green"))
lgd <- Legend(title = "Legend",
title_position = "topcenter",
at = seq(1,9,1),
col_fun = col_princ,
legend_width = unit(18, "cm"),
direction = "horizontal",
labels_gp = gpar(fontsize = 10),
nr = 1)
draw(lgd, just="center", x=unit(12,"cm"), y=unit(10, "cm"))
Thank you very much, i'm blocked
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)
So I've created a barplot for some data I have with the following code:
plot <- barplot(data,
beside = TRUE,
col = c("red", "blue", "green"),
space = c(0, 0.4),
width = 0.2,
xlim = c(0, 2),
ylim = c(0, 1.1),
legend = c("KNN", "MF1", "MF2"),
args.legend = list(x = 2.7, y = 1.2),
yaxt = 'n',
xpd = TRUE,
srt = 90
)
text(x = plot,
y = data + 0.05,
labels = as.character(round(data, digits = 2)),
srt = 90,
xpd = TRUE
)
I think the plot looks neat, but... It does not fit the region (I think at least). Are there any ways to keep the width of the bars in picture 1 and still show all 8 groups? My solution so far is to reduce the width of the bars as shown in picture 2.
Picture 1
Picture 2
I use VennDiagram to make a venn diagram with the following example code:
library(VennDiagram)
venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename = "trial2.emf");
Which gives this figure:
The names on the left and right of the figure are cut off, and a little bit of the name at the bottom as well. I tried changing width, but that makes the venn diagram itself get wider, and the names still get cut off.
How can I make the VennDiagram so that it includes the full names, either by adding more whitespace on the left and right of the diagram, or by pushing the names more towards the venn diagram?
You can justify the label text with cat.just. The package reference manual gives info. on how to pass the parameters.
For your example i used trial and error for the justification values.
# Plot
v <- venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename=NULL, cat.cex=0.8,
cat.just=list(c(0.6,1) , c(0,0) , c(0,0) , c(1,1) , c(1,0)))
grid.newpage()
grid.draw(v)
Another option (if a bit of a quick hack) would be to remove the cat.just argument and set a smaller grid::viewport. You may need to tweak the width of your graphics window / output device (ie pdf(..., width=...)):
# Plot
v <- venn.diagram(list(shams_90d = 1:3, shams_90d_4h = 2:4, sham3__shams_90d = 3:5,
sham3_90d__shams = 5:7, sham3_90d__shams_4h = 6:9),
fill = c("red", "green", "blue", "yellow", "purple"),
alpha = c(0.5, 0.5,0.5, 0.5, 0.5), cex = 1,cat.fontface = 2,
lty =1, filename=NULL, cat.cex=0.8)
grid.newpage()
pushViewport(viewport(width=unit(0.8, "npc"), height = unit(0.8, "npc")))
grid.draw(v)