I have a bunch of series I'm graphing with dygraphs, but one of the series is special and I bold it
dySeries("meanOfMeans",strokeWidth = 4) %>%
but if I try
dySeries("meanOfMeans",strokeWidth = 4,color = "black") %>%
it turns every line in the graph black. Is there a way to just set one color?
Alternatively, is there a way to order the series first? right now the special series is showing up last, which means when the number of series changes, the meanOfMeans falls into a different slot, changing it's color. This is distracting.
If it can just be ordered in the first column, this would be a non-issue, because the color wouldn't change. Note that the series is in the first column in the actual data.table itself, but when I add the dySeries() line, it moves to the end.
You'll need to specify the color you want for each variable separately with a dySeries command for each.
As specified on page 20 of the dygraph package manual, "global and per-series color specification cannot be mixed."
https://cran.r-project.org/web/packages/dygraphs/dygraphs.pdf#page=20
As you found, if you specify only one color, regardless of what series it was specified for, dyGraph will color all of your series accordingly. If you specify some colors, but not all, the colors will not be correct.
As for the ordering of variables, variables will appear on the dygraph according to their dySeries command. When using the stacked version, dyOptions(stackedGraph=TRUE), the variable that is specified first will appear on the top, and the last will appear on the bottom.
I hope this helps :-)
Related
I'm currently struggling with adding a border line around groups of regions with the same value regarding a specific variable. More explicitly: I plotted US-counties and used col.regions based on their dma_index (variable). The next step would be to add a line surrounding the areas containing counties with the same value of dma_index, i.e. those which are coloured the same (to make it more visible and make comparison with different variables more easy). Is there any way to accomplish this? Thanks a lot in advance!
Similar question to: R mapview: Point border color by group
I am working on a script for plotting volcano plots for a specific number of groups that the user specifies, i.e. if he wants to have five groups colored, the script modifies the data set accordingly with a new column called color. All the other observations that are there shouldn't be highlighted specifically, but be grey.
Is it possible to only specify the color of one group while letting ggplot2 choose all the colors for the other groups available?
I couldn't solve this using scale_color_manual, since it expects values for all groups!
This image demonstrates my problem, all other features except the groups, i.e. the "Features (all)" group should be grey instead of red, however I want ggplot to color all other groups automatically...
Specifying a custom color palette and sorting the color column solved my problem :)
My head is getting sore from me banging it so much.
I have a time-series that I've converted into an xts object w/ 7 variables. Now I'm trying to plot 4 of them, all price indices, on the same graph. I used autoplot (from the ggfortify package) to initialize the graph, and this is where the trouble begins.
Autoplot doesn't seem to work unless I give it at least one variable to plot. That's fine, but the two customizations I want for the variable -- its color and line type -- seem to have no effect.
But once I create the plot this way, I have little trouble adding the other 3 variables by adding geom_lines. Here's sort of what the code looks like:
p <- autoplot(foo.xts,xlab="Year",
ylab="Price Index",
columns="Variable1",linetype=4) # the linetype accomplishes nothing
p <- p + geom_line(aes(y="Variable2", color="green", linetype="solid"
# etc. for the other 2 variables
p # The 3 added variables do get the selected colors & line types.
But how can I customize the line for the first variable?
Then there's another problem in that I can't get a legend to appear. Here's how I'm trying to do that:
p <- p + scale_color_discrete(
name="Price Indices",
breaks=c("Variable1", "Variable2", "Variable3", "Variable4"),
labels=c("Index 1", "Index 2", "Index 3", "Index 4"))
This seems to accomplish nothing.
One thing I'd add is that in my various experiments trying to get the legend to work, I've sometimes gotten two sets of keys: one for colors and one for line types. This is obviously not what I'm after.
If someone could help me with this, I'd be forever in your debt!
I spent yesterday away from the computer, and when I returned in the evening fixed the problems. Here's how:
Stopped using autoplot. It's a classic case of hand-holding that throws you over the cliff. In other words, it automatically formats the plot in ways that are difficult (impossible?) to customize. Instead, ggplot makes the initial plot.
Since I'm making a series of plots, moved all the shared features to a separate, preamble section. This section creates a base plot, sets the x-axis variable (the date of the observation), labels the x-axis, and formats its tick marks. It also sets up standardized colors, line styles, and shapes to be used by all the "production" plots.
To set up the standardized elements, it uses scale_color_manual, etc. Each one has to be identical in all respects except those that are unique to its specific aesthetic attribute. E.g., scale_color_manual uses values like "red" whereas scale_linetype_manual uses values like "solid." Each manual setting includes the following elements: legend.title*, values, labels, and guide = guide_legend()*. (Items marked with * must be identical, otherwise you'll get different legends for each one.) For each plot, the actual legend title is first stored in a variable, legend.title, and then used in all the manual scale setting. This way the manual settings can be moved to the common section, but each plot has is own unique title for its legend.
3A. Actually, I was wrong about this. I was thinking LaTeX, where most things are evaluated where they appear at execution time. So a scale_color_manual statement at the start could change later on just by changing the value of legend.title. But in R, things are evaluated sequentially, and changing legend.title after the scale_color_manual statement is executed will have no effect. I worked around this by defining several variables in the preamble (e.g., one with the colors I'm using) and then using these variables in the various source_x_manual statements. This way, the only thing that change is the legend title.
Then each production plot starts by copying the base plot, labeling the y-axis, and then adds the geometric objects that it needs.
This approach has several advantages. 1) It modularizes the plotting so that problems are easier to isolate and solve, and most solved problems in the preamble section are solved for all plots. 2) It standardizes the plots, ensuring that their common features are formatted identically. 3) It reduces each production plot to a few statements; since this is the unique part for each plot, creating a new style of plot becomes relatively easy. 4) The value added by autoplot becomes minimal because this approach, separating shared elements in a preamble, compensates by isolating reusable code and the preamble, once debugged, allows much more fine-grain customization.
If you have any questions, please feel free to ask.
I have a sparkline:
BusinessDate<-c("01-01-2014","01-02-2014","01-03-2014","01-04-2014","01-05-2014")
Corn<-c(1000000000,2,3,.0000000005,4)
Wheat<-c(2000000,1,8,10,.111111115)
risk<- data.frame(BusinessDate, Corn, Wheat)
sparklines(risk[,c("Corn", "Wheat")],
times=as.numeric(risk$BusinessDate),
ptopts=('min.max'), buffer= unit(1,'lines'),
outer.margin=unit(c(4,12,4,15),'lines'))
I have a few questions about the plot that you will see when you run the above code:
(1) How can I change the y-axis from scientific notation to non-scientific notation?
(2) How can I change the color of the maximium to Green instead of red so that the minimum is red but the maximum is green?
(3) How can I i change the data label of the ptopts to show in non-scientific notation. You will see the bottom plot has a data label that reads 2e +06. can I make it read 2000000 instead?
(4) Is it possible to color the points that are greater than 1 standard deviation of the time series. I image this would have to do with ptopts. But is it possible..if so how?
I know this is a lot of questions. Thank you!!
To get everything you ask for you will need to hack the code of sparkline ... not the same function as sparklines but rather a helper function. It has hard-coded format specs for the labels. I can get you the colors of the points:
sparklines(risk[,c("Corn", "Wheat")],
times=as.numeric(risk$BusinessDate),
ptopts=list(labels='min.max', gp=gpar(col=c("red", "green") )), buffer= unit(1,'lines'),
outer.margin=unit(c(4,12,4,15),'lines'))
Look at the code of the two functions (by typing their names and see that the formatting is being conducted within the grid-graphics framework:
libary(YaleToolkit)
sparklines
sparkline
I can get the labels to change but I have not figured out how to assign them properly in sequence to two different sets of points by supplying a labels.ch argument to the ptopts list.
I am writing a script that will generate plots of plot(survfit(Surv(time, event)~factor)).
The different survival curves will have a different color. How is the order of the color defined? is it levels(factor)? unique(factor)?
My point is that I would like to automatically script the legend labels and text. Can I safely I use levels(factor) as legend text?
I am sure this is documented somewhere but the help entry of ?plot.survfit is not very helpful?
Yes, the order of the color depends on levels(factor), and yes, you can use levels(factor) for your legend text.
This applies everytime you are dealing with a factor. There is also a number of functions for you to manipulate the levels of your factor if you need so, e.g. check ?relevel.