Both custom tickmarks AND custom fontface in ggplot2 - r

Apologies if this is straightforward or has already been answered, but I've searched high and low and haven't been able to find a solution anywhere.
I'm using ggplot2, trying to (1) specify custom tick marks to an axis which involve subscripts, and (2) trying to specify that some of these tick marks should be bold and others italic. I know how to do (1) and (2) separately, but when I try to do both together, only (1) succeeds.
y.labels<-c(expression(A[Subscript]),expression(B^Superscript))
y.face<-c("bold","italic")
testdf<-data.frame(x=rnorm(100),y=1:2)
Bold/italics don't appear when I supply custom labels:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)
theme(axis.text.y=element_text(face=y.face))
But when I let the labels take their default values, bold/italics do appear:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
theme(axis.text.y=element_text(face=y.face))
As far as I can tell, the problem doesn't have anything to do with subscripting/superscripting,
even though that is my aim, here (and which is why I'm passing the tick marks directly, rather than creating a factor variable with the desired labels, which does work). This doesn't work, either:
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=c("Custom1","Custom2"))+
theme(axis.text.y=element_text(face=y.face))
If anyone can explain what's going on, here, and provide a solution whereby I can do both, I'd be much obliged. Thanks.

plotmath doesn't honour fontface, you want to use bold() or italic() in your expressions,
y.labels<-c(expression(bold(A)[Subscript]),expression(italic(B)^Superscript))
ggplot(testdf,aes(x=x,y=y))+geom_point()+
scale_y_discrete(label=y.labels)

Related

Increase Labelsize in Julia Plots

I am creating a plot using Plots in Julia. I was trying to increase the size of the labels and also the values on the axes but couldn't find the right keyword arg (I thought it was annotationfontsize but that doesn't seem to work). Does anyone know the right arg?
I think you're looking for guidefontsize and tickfontsize. You can check all the axis attributes in the documentation. For example,
julia> plot(rand(10); xtickfontsize=5, xlab="Big label, small tick labels", xguidefontsize=20)
will give you something like

plot function type=ā€œnā€ is ignored for plot(y~x)?

I am trying to plot a graph of certain values against time using the plot function.
I am simply trying to change the representation of the dots, using the pch= function. However R is simply ignoring me! I have also tried removing the dots so that I can place labels instead, but when I type in type="n" it ignores that too!
I am using the exact same format of code that I have used for other plots but this time it just isn't cooperating.
If I specify other features such as the title or the x/y axis labels, it will add those in but it simply ignores the pch or type commands.
This is my basic code:
plot(Differences ~ Time, data=subsetH)
But if I run
plot(Differences ~ Time, type="n", data=subsetH)
or
plot(Differences ~ Time, pch=2, data=subsetH)
it keeps plotting the same thing.
Is there something obvious I have missed?
I just came across your question because I encountered the same thing - creating an empty plot did not work, as type='n' was always ignored (as well as other type specifications).
With the help of this entry: Plotting time-series with Date labels on x-axis
I realized that my date format needed to be assigned as "date" class (as.Date()).
I know your entry dates back a little bit already, but maybe it's still useful.

Initializing and customizing an autoplot r-project object

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.

Plot in R won't change color

This is probably going to sound like a really stupid question but my plot in R will not change color!
this is my line in R
plot(MasterModCfs$V3,MasterModCfs$V5,col="blue")
I have tried it with spaces and without, with different colors, reloading, everything I could think of. If it's important, MasterModCfs contains literally thousands of data values.
I did one of the examples just to check
cars <- c(1, 3, 6, 4, 9)
plot(cars)
plot(cars, col="blue")
and it's blue. So that works.
Why won't my plot change colors?
Googling the same problem, I came across this present page - but subsequently found the source of my own issue through trial and error so posting here in case it helps. My x axis values were set up as factors - when I re-converted them to a normal string using as.character(), I was able to re-apply my own colours finally.
Without knowing what your par() settings are and what the classes of your columns, it's impossible to say.
You might try adding 'pch=21, bg="red"' to your plot command and see if that changes anything. It might give you a hint.
I suggest using the aes command in ggplot2. You can try
ggplot(data = MasterModCfs) + geom_point (mapping =aes (x=V3, y=V5), color="blue")
That should work!
Using plot directly I am not sure how to do it. I try your simple example (with cars) but it also fails if you add another vector for the "y" axis (as you wish to do in the end). To be honest, I have no idea why it works using only "cars" and the color.

equivalent to MatLab "bar" function in R?

Is there an function in R that does the same job as Matlab's "bar" function?
R does have a "barplot" function in the library graphics, however, it is not the same.
The Matlab bar(X,Y) (verbatim excerpt from MATLAB documentation) "draws a bar for each element in Y at locations specified in X, where X is a vector defining the x-axis intervals for the vertical bars." (emphasis mine)
However, the R barplot function does not allow one to specify locations.
Perhaps there is a method in ggplot2 that supports this? I am only able to find standard bar charts in ggplot2.
No, barplot is not the same as bar, but you should read the whole help. You can do many things to position the bars. The first is simply their order in Y. You could insert spaces if you wish (additional 0s). If you have X and Y then sort Y on X (Y[order(X)]) and plot it. If you need to change positions use the "space" and "width" arguments. It's not as straightforward as specifying X values I suppose but it's definitely more useful in most situations. Generally what you want to adjust is widths of bars and spaces between bars. Their position on the X-axis should be arbitrary. If the position on the X-axis is really meaningful then you should be using line plots, not bar graphs.
In R:
barplot(rbind(1:10, 2:11), beside=T, names.arg=1:10)
In MATLAB:
>> bar(1:10, [(1:10)' (2:11)'])
Read up on par . Then observe, for example:
x<-c(1,2,4,5,6)
y<-c(3,4,3,4,2)
plot(x,y,type='h',lwd=6)
Edit: yes, I know this doesn't (yet) plot multiple data sets, but I would hope you can see simple ways to make that happen, with spacings, colors, etc. specified to your exact liking :-)
Sounds vaguely like the R stepfun. On the other hand one would need to know what "draws a bar" means before saying it is not the same as barplot(..., horiz=TRUE) One would, of course, need to examine some more detailed evidence such as data and plots before arriving at a conclusion, however. #John Colby should be congratulated for adding some specificity to the discussion. The axis function is probably what Quant Guy needs education regarding.

Resources