I am trying to make a parallel coordinate graph in R. I want the user to be able to adjust which specific factor level they are viewing data for (album name in this case). Below is my code:
ggparcoord(ts,
columns = c(3:4, 9, 11:12),
groupColumn = 20,
alphaLines = .75,
showPoints = TRUE,
scale = "globalminmax") +
facet_grid(~ album_name) +
scale_color_manual("album_name",
values = c("#50a7e0", "#ddc477", "#923c81", "#951e1a", "#876b79", "#353839",
"hotpink", "#bababa", "#994914", "#f6ed95", "#951e1a"),
labels = levels(ts$album_name)) +
theme_light() +
theme(legend.position = "none")
The output looks like this:
As you can see, it is very convoluted and the viewer can't glean much info from it. I want to make it to where the user can select which individual plot they would like to view, without having to use a shiny dashboard (htmlwidgets would be ideal, but I do not know how to implement this well). I am also open to a non interactive solution that might improve readability for the viewer.
Related
I’m trying to use facet_wrap in r with 4 plots that have different x-axis. I used scales = “free” which was helpful, but most of the plots touch the top of the graph. I would like there to be space above the tallest bar of the graph, so it doesn’t look like it’s going off above the limit of the graph. I hope that makes sense - this is my first time posting a question here. I have provided the code that I have and a screenshot of one of the graphs. I haven't been able to find any sort of fix without using cow plot which is a bit more advanced than I would like.
Code:
wrap_plot <- df %>% ggplot(aes(x=Race, y = Rate, fill = Sex))+
geom_col(position = "dodge")+
labs(x = "Race/Ethnicity",
y = "Rate per 100,000 population")+
theme_classic()+
facet_wrap(~Disease, scales = "free")+
scale_y_continuous(expand = c(0, 0))
I have a plot with a geom_area() section that shows whether an event is active or not at a certain step in a simulation (thanks to this community for helping me with that too!).
It works well when I am only showing one run of the simulation, however I need to be able to show more than one run at a time, to see any differences between the timing of this event in different runs.
I set the fill of geom_area() to correspond to the run number, and the position to position_dodge(0) to be able to overlap the areas so that the denser the colour, the more runs had an event active at that point. The only problem is that I am not interested in differentiating the runs, so I would like them to have all the same colour. I managed to do it with a bit of a hack, by simply making a palette that had grey 10 times, however this is not something I can do manually since I will have graphs with 100s of runs soon.
How can I make all factors the same colour regardless of how many there are? Or if there is a better solution to show a plot like the one below, what is it?
Including the plot I managed so far (which is exactly what I need, only scalable) and the code I used to make it:
ggplot(df,aes(x = step, y = count)) +
geom_area(data = event, aes(x = step, y = pop_size * event, fill = as.factor(run_num)),
inherit.aes = FALSE, position = position_dodge(0), alpha = 0.2, show.legend = FALSE) +
geom_point(aes(color = breed), alpha = 0.7, size = 1) +
scale_color_brewer(palette = "Set3") +
# horrible hack incoming
scale_fill_manual(values = c("lightgrey","lightgrey","lightgrey","lightgrey","lightgrey",
"lightgrey","lightgrey","lightgrey","lightgrey","lightgrey")) +
coord_cartesian(ylim = c(0, pop_size))
Solved this thanks to the comment by d-a-wells above, who suggested using scale_fill_manual(values = rep("lightgrey", n_simulations)) to ensure the palette covers however many factors I have. Can't believe I didn't think of it!
Hi I'm looking for a way to scale a ggplot2's legend (independently of the plot and label sizes), and ideally an automated way to do this scaling so that it fits on the page.
It is very easy to scale the whole plot, including the legend, just by changing the output size but sometimes (especially if I am pasting the plots into a report at a fairly small size) I might need the axis sizes to be fairly large.
I can't find any way to scale the legend as a whole. The options seem to be to manually split it over several rows or to change each element of the legend independently (as I have attempted below); but some of these can be set using rel and some can't its hard to know what you'll end up with and it still looks a bit funny.
It seems strange to me that the default behaviour for ggplot2 is to allow legends to go off the page.
Basically I want the plot like this (which was made in paint using a combination of test.png and test2.png from my example below):
Ideally I'd like it if during the save process it figures out the plot widow and then applies some scaling automatically. Otherwise if I could scale down with a percentage that what be second best option.
Reprex of a simple example. WARNING it saves to a c:\temp folder
library(ggplot2)
testplot <- ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, colour = Species))+
geom_point()+geom_hline(aes(yintercept = 6, colour = "example line"))+theme(legend.position = "bottom")
ggsave(testplot,filename = "C:/temp/test.png", width = 3, height = 3)
ggsave(testplot,filename = "C:/temp/test2.png", width = 6, height = 6)
test2 <- testplot + theme(legend.key.size = unit(0.5,"lines"), legend.text = element_text(size = rel(0.5)),
legend.title = element_text(size = rel(0.5)))+
guides(colour = guide_legend(override.aes = list(size = 0.6)))
ggsave(test2,filename = "C:/temp/test3.png", width = 3, height = 3)
Created on 2019-09-04 by the reprex package (v0.3.0)
This is probably a pretty naive question. I searched and couldn't find a duplicate, but please let me know if this has already been asked!
I have some functions that, based on a boolean argument, either make a new plot or plot ontop of the existing graph. Here's a MWE:
plotThing <- function(boolPoints = FALSE, color = "black") {
x <- sample(c(1:100), size = 10, replace=TRUE)
y <- sample(c(1:100), size = 10, replace=TRUE)
if(boolPoints) {
points(y~x, col = color, pch = 19)
}
else {
plot(y~x, col = color, pch = 19)
}
}
This style of function has been useful for me because I import all of these functions into a markdown document, and then it's really easy to just plot a arbitrarily large amount of points on one plot. For example, in the markdown document:
```{r MWE}
source("MWE.R")
plotThing();
plotThing(boolPoints = TRUE, color = "red")
legend("topright", title = "Things", c("thing1", "thing2"), pch = c(19,19), col = c("black", "red"))
```
In the actual code, I think this style might be a good choice because my plotting functions are pretty large and have lots of arguments that would be ugly to repeat over and over again.
Is it possible/how would I have an equivalent setup with ggplot? Or is this just poor design that you'd recommend changing?
I am not sure whether this will directly answer your question, but I think it might help.
So, in ggplot2 you have the nice feature of being able to save plots without printing them:
p <- ggplot(mtcars, aes(x=hp, y=mpg)) + geom_point()
Now, you could - based on a condition - add further points to the same plot if you have gathered more data and print it or just print the basic plot:
if(addPoints){
print(p + geom_point(data = data.frame(hp=500, mpg=50)))
} else {
print(p)
}
However, if you add additional data to the plot you would just have to make sure that the aesthetic mapping matches.
Im making a treemap of some data using a pretty cool library called treemapifyof which the details can be found here and github repository here
Based on my reading of the documentation it seems to be based on ggplot2 so it should be possible to modify the graph using the grammar of graphics
My code is below with some made up data. The end result is pretty nice but i want to change the color scheme to a more subtle using the line scale_colour_brewer. The graph runs fine but the colour scheme seems to be ignored. Has anyone had any experience with this?
# Create Random Data
country <- c("Ireland","England","France","Germany","USA","Spain")
job <- c("IT","SOCIAL","Project Manager","Director","Vice-President")
mydf <- data.frame(countries = sample(country,100,replace = TRUE),
career = sample(job,100,replace=TRUE),
participent = sample(1:100, replace = TRUE)
)
# Set Up the coords
treemap_coords <- treemapify(mydf,
area="participent",
fill="countries",
label="career",
group="countries")
# Plot the results using the Green Pallete
ggplotify(treemap_coords,
group.label.size.factor = 2,
group.label.colour = "white",
label.colour = "black",
label.size.factor = 1) +
labs(title="Work Breakdown") +
scale_colour_brewer(palette = "Greens")
If you want to change the fill color of the rectangles, try the scale for fill instead the one for colour:
scale_fill_brewer(palette = "Greens")