Can't change svg attr in d3.js - css

I tried to change width attribute of svg in d3.js dynamically like this.
var vis = d3
.select("#chart")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
vis.attr("width", 2000)
This seems not working. What is the proper way of setting attr.
Thanks.

Related

ggplot2 cuts off parts of my figure when zooming in

I am trying to make a figure in gglot2 that looks something like this.
However, I seem right now to have a trade off between having all the squares small like ... Or zooming in on the squares and having parts cut
both displayed here as I may not add more pictures, yet
My code is as follow
if (!require('ggplot2')) install.packages('ggplot2'); library('ggplot2')
Odds <- c(1.2,1,0.97,1,1.38,0.95,0.85,0.95)
x <- c(5,3.5,0,-3.5,-5,-3.5,0,3.5)
y <- c(0,3.5,5,3.5,0,-3.5,-5,-3.5)
summed <- data.frame(Odds,x,y)
d <- qplot(x, y, data=summed, colour =Odds)
d + theme_classic(base_size = 14) + geom_point(size = 30, shape=15) +
scale_colour_gradient(low="grey", high = "black") +
ylab("") +
xlab("") +
scale_y_continuous(breaks=NULL) + scale_x_continuous(breaks=NULL)
I hope some of you can help me.
GGplot grants a certain "bleeding" or "breathing" around plot area. The size of your geom_point simply goes beyond that space.
A solution is to set custom limits to the ploting area. Try this:
d <-qplot(x, y, data=summed, color =Odds)
d + theme_classic(base_size = 14) + geom_point(size = 30, shape=15) +
scale_colour_gradient(low="grey", high = "black") +
ylab("") +
xlab("") +
scale_y_continuous(breaks=NULL) +
scale_x_continuous(breaks=NULL) +
theme(aspect.ratio = 1) + ## Optional. Ensures you get a square shaped plot
expand_limits(x =c(min(x) - 1, max(x) + 1), ## Expands the limits, reads from your predefined "x" and "y" objects.
y =c(min(y)-1, max(y) +1))

Change size of a line plot, understand how the size argument works

I'm making a multiple lines plot with errorbars. If I don't use the size argument, everything is fine:
# sample data
Response=runif(4)
ResponseMin=Response-Response/5
ResponseMax=Response+Response/5
Cases=rep(c("Case1","Case2"),each=2)
df=data.frame(x=1:2,Average=Response,Lower=ResponseMin,Upper=ResponseMax,Case=Cases)
# let's plot
library(ggplot2)
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case)) +
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
labs(y="foo",title="Some plot fu")
However, when I modify the line size, I start getting weird stuff:
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 1)) +
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
labs(y="foo",title="Some plot fu")
Why the extra legend entry "1"? And when I add a size argument for the errobars, it looks like the size of the lines stays the same, whatever the value of size is:
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 1)) +
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25, size = 1)) +
labs(y="foo",title="Some plot fu")
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case, size = 2)) +
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25, size = 2)) +
labs(y="foo",title="Some plot fu")
Can you help me figure out what's happening here?
If you set size inside aes you are mapping it to a variable
`1` = 1
and ggplot2 creates a legend. If you just want to set the size, you can do that outside of aes:
geom_line(aes(group=Case), size = 1)
try this, size outside aes()
ggplot(df,aes(x=x,y=Average,colour=Case)) +
geom_line(aes(group=Case), size = 1) +
geom_point() +
geom_errorbar(aes(ymin=Lower,ymax=Upper,width=0.25)) +
labs(y="foo",title="Some plot fu")

Increase plot size (width) in ggplot2

Below is a plot that I want to include in a paper. The problem is the width of my plot which is to small (that make x-axix not readable at all)
Here is the ggplot2 code myCode.r :
require("ggplot2")
all <- read.csv(file="benchmark/bench.query.csv", head=TRUE, sep=";")
w <- subset(all, query %in% c("sort.q1", "sort.q2", "sort.q3", "sort.q4", "sort.q5"))
w$rtime <- as.numeric(sub(",", ".", w$rtime, fixed=TRUE))
p <- ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore))
p <- p + scale_shape_manual(values = 0:length(unique(w$triplestore)))
p <- p + geom_point(size=4)
p <- p + geom_line(size=1,aes(group=triplestore))
p <- p + labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))")
p <- p + scale_fill_continuous(guide = guide_legend(title = NULL))
p <- p + facet_grid(trace~type)
p <- p + theme_bw()
ggsave(file="bench_query_sort.pdf")
print (p)
I've look around to see how to enlarge the plot, but I found nothing.
Any idea about what to add/delete/modify in my code ?
Inside a Jupyter notebook I found the following helpful:
# Make plots wider
options(repr.plot.width=15, repr.plot.height=8)
Probably the easiest way to do this, is by using the graphics devices (png, jpeg, bmp, tiff). You can set the exact width and height of an image as follows:
png(filename="bench_query_sort.png", width=600, height=600)
ggplot(data=w, aes(x=query, y=rtime, colour=triplestore, shape=triplestore)) +
scale_shape_manual(values = 0:length(unique(w$triplestore))) +
geom_point(size=4) +
geom_line(size=1,aes(group=triplestore)) +
labs(x = "Requêtes", y = "Temps d'exécution (log10(ms))") +
scale_fill_continuous(guide = guide_legend(title = NULL)) +
facet_grid(trace~type) +
theme_bw()
dev.off()
The width and height are in pixels. This is especailly useful when preparing images for publishing on the internet. For more info, see the help-page with ?png.
Alternatively, you can also use ggsave to get the exact dimensions you want. You can set the dimensions with:
ggsave(file="bench_query_sort.pdf", width=4, height=4, dpi=300)
The width and height are in inches, with dpi you can set the quality of the image.
If you are using RMD(R Markdown) this would be the easiest way to define width and height.
```{r fig.align="center", echo = FALSE,fig.width = 14}
<write the code for your plot here>
```
Note: options() not worked for me so I used this method

Add sequential arrows to a ggplot bubbleplot

I have a line plot that I have placed an overlay of bubbles onto. Before I overlaid the bubbles, I was able to connect each point with an arrow to show the sequential relationship using this advice
But now that I have overlaid my bubbles, I still want to connect each bubble by an arrow as previous.
Here is my data:
X <- c(-0.373,-0.256,-0.272,0.048,0.219,0.313,0.209,0.112)
Y <- c(-0.055,-0.091,0.100,0.153,-0.139,-0.004,0.040,-0.004)
Size <- c(37,31,25,10,5,4,6,10)
Label <- c(1,2,3,4,5,6,7,8)
DF <- data.frame(X,Y,Size,Label)
Using the above advice, I can try and draw a plot with arrows connecting each bubble, but the size of the bubbles obscures the arrow heads.
ggplot(DF,aes(x=X, y=Y, size=Size,label=Label),legend=FALSE) +
geom_segment(aes(xend=c(tail(X,n=-1),NA), yend=c(tail(Y,n=-1),NA)),
size=0.3, arrow=arrow(length=unit(0.3,'cm'))) +
geom_point(color='darkblue',fill="red", shape=21) +
geom_text(size=2) +
theme_bw() +
scale_size(range = c(4, 30), name="Size", breaks=c(10,25,50),
limits = c(1, 100))
I would basically like the above plot, but with the arrow heads visible. I know it is possible to write the arrows overtop the bubbles so I can see each arrow, but that is not what I am looking for. What I would like would be an arrow drawn from the outer edge of one bubble to the outer edge of the next bubble. So I need someway to shorten the head of each arrow by the radius of bubble it is pointing to.
And I have no idea why I get the warning at the end
Removed 1 rows containing missing values (geom_segment).
You can start with the following:
Size_penalty <- 1000
X <- c(-0.373,-0.256,-0.272,0.048,0.219,0.313,0.209,0.112)
X_next <- c(X[-1], NA)
Y <- c(-0.055,-0.091,0.100,0.153,-0.139,-0.004,0.040,-0.004)
Y_next <- c(Y[-1], NA)
Arrow_length <- sqrt((X - X_next)^2 + (Y - Y_next)^2)
Size <- c(37,31,25,10,5,4,6,10)
Size_next <- c(Size[-1], NA)
X_begin <- X + Size / Size_penalty * (X_next - X) / Arrow_length
Y_begin <- Y + Size / Size_penalty * (Y_next - Y) / Arrow_length
X_end <- X_next + Size_next / Size_penalty * (X - X_next) / Arrow_length
Y_end <- Y_next + Size_next / Size_penalty * (Y - Y_next) / Arrow_length
Label <- c(1,2,3,4,5,6,7,8)
DF <- data.frame(X, Y, X_begin, Y_begin, X_end, Y_end, Size, Label)
ggplot(DF, aes(x=X, y=Y, size=Size, label=Label),legend=FALSE) +
geom_point(color='darkblue', fill="red", shape=21) +
geom_segment(aes(x=X_begin, y=Y_begin, xend=X_end, yend=Y_end),
size=0.3, arrow=arrow(length=unit(0.3, 'cm'))) +
geom_text(size=4) +
theme_bw() +
scale_size(range = c(4, 30), name="Size", breaks=c(10, 25, 50),
limits = c(1, 60))
Here I use Size / Size_penalty as a proxy to bubble radius, which is obviously quite far from being elegant. But this is the best I can do, since there's a scale_size, so that conversion from size to radius is implicit. All that is left is to find a conversion function like
rad <- function(ggplot_size_after_scaling) {}

Error bars show through open symbol

I have a plot with pairs of points that are slightly offset. Each pair of points has associated error bars. I have specified that the symbol of the first point in the pair is different from that of the second (closed circle vs open circle). I would like it so that the error bars do not show through the open symbol.
Here is a mock data set:
x = runif(4,-2,2)
x_1 = runif(4,-1,3)
dfr <- data.frame(
x = c(x, x_1),
y = rep(c("A","B","C","D"), 2),
upper = c(x+2, x_1+1),
lower = c(x-2, x_1-2),
type = rep(c("alpha", "beta"), each = 4))
And here is the plot:
dodge=position_dodge(width=0.5)
ggplot(dfr,aes(x=y,y=x,colour=type)) +
geom_point(size=8,aes(shape=type),position=dodge) +
geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) +
scale_colour_manual(values = c('gray','black')) +
scale_shape_manual(values = c(19,21)) +
coord_flip() +
opts(legend.position="none")
Thanks for any help you can provide!
I can't think of a way to make an 'open' point and not let the errorbar show trough. The only way of doing this would be to fill the points with the same colour as the background, but then your gridlines won't be visible through the point.
To do this, map the fill aesthetic to type, and specify scale_fill_manual with the fill colour grey90 which is the theme_grey setting:
ggplot(dfr,aes(x=y,y=x,colour=type, fill=type)) +
geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) +
geom_point(size=8,aes(shape=type),position=dodge) +
scale_colour_manual(values = c('gray','black')) +
scale_fill_manual(values=c('grey', 'grey90')) +
scale_shape_manual(values = c(19,21)) +
coord_flip() +
opts(legend.position="none")
Why don't you just use color as shown in the modified code below. It will fill the black circles too. Not sure if that is acceptable.
ggplot(dfr,aes(x=y,y=x,colour=type)) +
geom_point(size=8,position=dodge) +
geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) +
scale_colour_manual(values = c('gray','black')) +
coord_flip() +
opts(legend.position="none")

Resources