dankelley / plan

R package for project planning
https://dankelley.github.io/plan/
33 stars 9 forks source link

legend not on the righttop of the plot #26

Closed JuqiangJ closed 4 years ago

JuqiangJ commented 4 years ago

I try to reproduce the plot with the code from the example but the legend cannot be place on the topright corner.

library("plan")
g <- new("gantt")
g <- ganttAddTask(g, "Courses") # no times, so a heading
g <- ganttAddTask(g, "Physical Oceanography (A)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Chemistry Oceanography (B)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Fluid Dynamics (A+)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Biological Oceanography", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Geological Oceanography", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Time-series Analysis", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Research") # no times, so a heading
g <- ganttAddTask(g, "Literature review", "2016-09-03", "2017-02-01", done=20)
g <- ganttAddTask(g, "Develop analysis skills", "2016-09-03", "2017-08-01", done=30)
g <- ganttAddTask(g, "Thesis work", "2016-10-01", "2018-07-15", done=30)
g <- ganttAddTask(g, "Thesis proposal", "2017-05-01", "2017-06-01")
g <- ganttAddTask(g, "Writing (papers & thesis)", "2017-03-01", "2018-07-15")
g <- ganttAddTask(g, "Defend thesis", "2018-09-01", "2018-09-05")

plot(g, ylabel=list(font=ifelse(is.na(g[["start"]]), 2, 1)),
     event.time="2018-08-13", event.label="Report Date")

par(lend="square") # default is round
legend("topright", y = 1,
       pch=22,
       pt.cex= 1, 
       pt.bg=gray(c(0.3, 0.9)),
       border="black", 
       legend=c("Completed", "Not Yet Done"), 
       bg="white")

plot_zoom

dankelley commented 4 years ago

The example is confusing because it has both "topright" and y given. Perhaps you can tell me precisely where you got that example, and I can fix it.

Anyway, using "topright" does not quite work because the plot is extending past the region 0<=x<=1, 0<=y<=1. The best plan, not just here but in any case, is to specify x and y yourself. In this case, for example, we have a vertical line that will go under the legend, which might be confusing, so I would suggest placing the legend at the centre.

So, I've modified your code as below, and it seems to work for me. Is it okay for you?

(PS. I modified your comment to put triple-backtick-R at the start and triple-backtick at the bottom, so github will format it as R, which is much easier to read than lines of text. Github provides docs on formatting.)

png("plan.png")
library("plan")
g <- new("gantt")
g <- ganttAddTask(g, "Courses") # no times, so a heading
g <- ganttAddTask(g, "Physical Oceanography (A)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Chemistry Oceanography (B)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Fluid Dynamics (A+)", "2016-09-03", "2016-12-05", done=100)
g <- ganttAddTask(g, "Biological Oceanography", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Geological Oceanography", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Time-series Analysis", "2017-01-03", "2017-04-05")
g <- ganttAddTask(g, "Research") # no times, so a heading
g <- ganttAddTask(g, "Literature review", "2016-09-03", "2017-02-01", done=20)
g <- ganttAddTask(g, "Develop analysis skills", "2016-09-03", "2017-08-01", done=30)
g <- ganttAddTask(g, "Thesis work", "2016-10-01", "2018-07-15", done=30)
g <- ganttAddTask(g, "Thesis proposal", "2017-05-01", "2017-06-01")
g <- ganttAddTask(g, "Writing (papers & thesis)", "2017-03-01", "2018-07-15")
g <- ganttAddTask(g, "Defend thesis", "2018-09-01", "2018-09-05")

plot(g, ylabel=list(font=ifelse(is.na(g[["start"]]), 2, 1)),
     event.time="2018-08-13", event.label="Report Date")

par(lend="square") # default is round
## Place the legend with x and y coordinates.  Note that using
## locator(1) and clicking on the plot can be a good way to
## find a location, although simply starting with x at about
## 0.5 and y about 1 will be close, for e.g. a centred legend
## at the top of the plot.  Note that the plot region actually
## extends past the y range from 0 to 1, and so we need to
## set par(xpd) to permit drawing outside par("usr").
par(xpd=NA)
legend(x=0.55, y=1.05,
       pch=22,
       pt.cex= 1, 
       pt.bg=gray(c(0.3, 0.9)),
       border="black", 
       legend=c("Completed", "Not Yet Done"), 
       bg="white")
dev.off()
JuqiangJ commented 4 years ago

thanks! yes, it works!