Dodged Bar Plots in ggplot - Defining colour by X axis as opposed to fill - ggplot2

Having some issue with dodged bar charts in ggplot. Essentially the only way I can dodge them is by changing fill to another variable, but I want to colour the paired plots by what they are on the x axis.
Have included a dummy picture of what I am trying to achieve.
I'm not sure if I'm missing something obvious here.
Many thanks in advance!

One trick is to use another not so useful variable to allow the dodge to happen. In the example below, I use color to separate them, but giving all of them the same color:
X = data.frame(Year=factor(rep(c(2016,2018),3)),
Sales=runif(6),Brand=rep(c("Toyota","Skoda","Ford"),each=2))
ggplot(X,aes(x=Brand,y=Sales,fill=Brand)) +
geom_col(aes(col=Year),position="dodge",show.legend=FALSE) +
scale_color_manual(values=c("black","black")) +
geom_text(aes(label=Year,y=0.5*Sales),position=position_dodge2(1),angle=90)

Related

How do I correctly describe this 4x4 square in my K-map?

I am trying to find a (SoP)-expression using the embedded K-map. I have a box of size 4x4 which is a permitted use however I am having a hard time understanding how I could implement it.
To me the 4x4 box represents that the output is always 1 independet on any of the variables. Then I'd like to use the 2x4 box to the right and produce:
1 OR (Qc AND !Qd), but this does not produce the correct result.
I can see several alternative ways to produce the correct result. My questions are specifically:
Why can't I use the 4x4 box, or perhaps, how do I represent it correctly?
How do I know when I can represent parts of the output as a 4x4 box?
Perhaps Im missing something more fundamental.
Thx in advance.
The point of placing rectangles in a K-map is to eliminate variables from an expression. When the result of a rectangle is the same for the variable values X and X', then the variable X is not needed and can be removed. You do this by extending an existing rectangle by doubling the size and eliminating exactly one variable, where every other variable stays the same. For the common/normal K-map with four variables this works with every such rectangle because in a way the columns/rows are labelled/positioned. See the following example:
The rectangle has eliminated the variables A and B, one variable at a time when the size of the rectangle has been extended/doubled. This results in the function F(A,B,C,D) = C'D'. But check the following K-map of four variables:
Notice that the columns for the D variable has been changed (resulting in a different function overall). When you try to extend the red rectangle to catch the other two 1 values as well, you are eliminating two variables at the same time (B and D). As you cannot grow the rectangle anymore, you are left with two rectangles, resulting in the function F(A,B,C,D) = BC'D' + B'C'D (which can be simplified to C' * (BD' + B'D)).
The practice in placing rectangles in the K-map isn't just placing the biggest rectangle possible, but to eliminate variables in the right way. To answer your questions, you can always start with the smallest rectangle and extend/double its size to eliminate one variable. See the following example:
The green rectangle grows in these steps:
Start with A'BC'D'E
Eliminate the (only) variable A by growing "down", resulting in BC'D'E
Eliminate the (only) variable D by growing "right", resulting in BC'E.
But now, the rectangle cannot grow/double its size anymore because that would eliminate the variable E, but also somehow eliminate the variable C. You cannot eliminate the variable E, because you have 0 values to the left of the green rectangle and 1 values to the right of the green rectangle (all in the left half of the K-map, where you have the value C'). The only way to increase/grow the rectangle is to get the "don't care" values to eliminate the B variable (not shown here).
The overall function for this K-map would be F(A,B,C,D,E) = C'E + DE' + CD' (from three 2x4 rectangles).

Change support and resistance line color in mplfinance

Currently, I'm using this code. How can I change the hlines to red if it's a resistance and blue if it's a support?
mplfinance.plot(df,
type = 'candlestick',
style = 'binance',
hlines=dict(hlines= support_resistance,linestyle='-', linewidths = (1,1)),
volume = True)
I'm getting results like this:
hlines=dict(hlines= support_resistance,linestyle='-',linewidths = (1,1),colors=('b','r')
See for example cell "In [6]" in this tutorial: https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb
You may need to include as many colors as you have support_resistance lines. So for example, maybe something like: colors=['b','r','b','r','r','r']
Regarding handling support resistance colors dynamically (per your comment) it is not appropriate for mplfinance to provide algorithms for determining support or resistance, only to provide tools to make it easier for you to visualize them.
Also, each user may have their own specific way of determining support or resistance.
Presumably as you are building the support_resistance list, at the point in your code where you are adding a specific price to that list, you probably know whether that price represents support or resistance. At the same point in your code you should add a color ('b' or 'r') to the colors list. That way you dynamic build two lists: support_resistance, and colors which end up being the same length.
Based on the suggestion from Daniel, I made a list of colors as follows and it worked. support_resistance variable here consists of both support and resistance levels.
colors = []
for lvl in support_resistance:
if lvl > df['Close'][-1]:
colors.append('r')
else:
colors.append('b')

How can I set the color for subsequent plot commands?

I have to issue many plot commands to create a picture. As in the following example:
color="C0-"
plt.plot([1,5,4], color)
plt.plot([3,7,8], color)
To simplify code I would prefer something like:
plt.set_color("C0-") # ERROR
plt.plot([1,5,4])
plt.plot([3,7,8])
Is this possible?
If no color argument is given, the color of the plots is determined by the color cycle in use. You may change the color cycle at runtime to only have one single color, which effectively makes all subsequent plots that same color.
plt.gca().set_prop_cycle('color', ["blue"])
To get the first color of the current color cycle you may use
c0 = plt.rcParams["axes.prop_cycle"].by_key()["color"][0]
plt.gca().set_prop_cycle('color', [c0])

axis orient / position with dimple.js

Is there a way to explicitly position/orient an axis using dimple.js? I know that the first x-axis is added to the bottom and the second is added to the top, so I can get the desired outcome like this:
var xAxis = myChart.addMeasureAxis("x", "dur");
xAxis.hidden = true;
xAxis = myChart.addMeasureAxis("x", "dur");
...but that seems a little hacked.
I don't think there's any way around that. Here's the relevant code : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L123
Because firstX is local to the draw function there isn't a way you could override it within the context of the loop.
You could possibly duplicate the logic used to position the top axis : https://github.com/PMSI-AlignAlytics/dimple/blob/master/src/objects/chart/methods/draw.js#L226
And then only add one axis (which would be positioned at the bottom), then manually move your axis after the drawing, like :
chart.draw();
chart.axes[0].shapes.attr('transform', ...);
But you would need to do this after every draw or else they'll be repositioned. The hack you have seems easiest.
You could open an enhancement ticket for this though : https://github.com/PMSI-AlignAlytics/dimple/issues?q=is%3Aissue+label%3Aenhancement+is%3Aopen

Are Gadfly plots currently composable?

Is there currently a way to add plot elements together in Gadfly.jl?
For example, in R if I have another function that returns a ggplot and I want to add a title to it, I'd do the following:
p <- makeMyPlot()
p + ggtitle("Now it has a title")
Is there currently a Gadfly equivalent? If not, is this on Gadfly's roadmap?
There is add_plot_element(), which can add stuff to an existing layer:
xs = [0:0.1:pi]
l = layer(x=xs, y=sin(xs))
add_plot_element(l, Guide.title("Now it has a title"))
You can then plot the layer using plot(l), and invoke either draw or display to actually show something. Further down, there's a bunch of overloads that work on a Plot directly:
p = plot(x=xs, y=sin(xs))
add_plot_element(p, Guide.title("Now it has a title"))
display(p)
I can't find either of these functions in the documentation, but fortunately the source is comprehensible enough. One of the many joys of Julia =)